I am working with forms in Express.js and I am using express-validator for validation. The code I have shown below is a "form" with two text boxes, a choose file button and a submit button. When the user submits the form, but there are validation errors I would like to:
Show an error message, saying which of the elements in the form the user needs to fill in. I believe I know how to do this with pop-up windows, but I would like to know if there is a way to "highlight" the text box that the user is required to fill in, or put a "* Required" text next to the text box/button.
Currently, if there are errors, after the submission, the two text boxes in the form are populated with the variables that were previously submitted by the user. I would like to do the same thing for the "Choose file" button. Using the "value" field as I have done in the two text boxes, does not work for the button. Is there way to do this?
//in routes folder
exports.myPage = function(req, res) {
res.render('myPage.jade', {valName: 'Enter', valLastName: 'Enter'});
};
exports.myPage_post_handler = function(req, res) {
nameID = req.body.nameID;
lastNameID = req.body.lastNameID;
my_file = req.body.my_file;
req.session.nameID = nameID;
req.session.lastNameID = lastNameID;
req.session.my_file = my_file;
if (typeof req.session.nameID != 'undefined'){
valName = nameID;
}
if (typeof req.session.lastNameID != 'undefined'){
valLastName = lastNameID;
}
req.assert('nameID', 'Please enter a name').notEmpty();
req.assert('lastNameID', 'Please enter a last name').notEmpty();
req.assert('my_file', 'Please enter a my_file').notEmpty();
var errors = req.validationErrors();
if( !errors){
res.redirect('/other_page');
}
else {
res.render('myPage.jade');
}
};
//in my_page.jade
form(method='post')
| Name:
div
input(type='text', name='nameID', id = 'name', value = valName)
| Session ID:
div
input(type='text', name='laastNameID', id ='lastName', value = valLastName)
| Video:
div
input(type='file', name='my_file', id='fileF')
div
input(type='submit', value='submit', value='Submit')
In Html5 (which also applies to Jade) you can put a "required" parameter whenever you declare text boxes or radio buttons etc..
Related
I have tried this:
<script>
converse.initialize({
websocket_url: 'wss://xxx.xxx.xxx/websocket',
jid:'xxxxx#xxxx.xxx.xxx',
show_controlbox_by_default: true,
view_mode: 'overlayed' });
</script>
I was hoping this would display a login form with the Username field already populated with the value given in jid. However converse.js still displays an empty field with the default placeholder.
I'm using "https://cdn.conversejs.org/4.0.1/dist/converse.min.js"
OK so I'm not a javascript programmer but the solution I've come up with is to modify the renderLoginPanel() function and add the following
renderLoginPanel() {
this.el.classList.add("logged-out");
if (_.isNil(this.loginpanel)) {
this.loginpanel = new _converse.LoginPanel({
'model': new _converse.LoginPanelModel()
});
const panes = this.el.querySelector('.controlbox-panes');
panes.innerHTML = '';
panes.appendChild(this.loginpanel.render().el);
this.insertBrandHeading();
} else {
this.loginpanel.render();
}
/* ***Add this line to pre-populate the username field*** */
document.getElementById("converse-login-jid").value = _converse.jid;
this.loginpanel.initPopovers();
return this;
},
I'd be interested to hear if this is anywhere near a good solution.
I am writing one small dialog box with multiple textboxes, and it contains input fields. I would like to show user's input values like a report or something for all entered values in a list in another dialog.
Here is my code snippet where I set label when user inputs values and presses Button. But when user adds some more additional values in a textbox, would it be possible to be able to see the already added values listed somewhere?
okAndMore = new Button("Add & More");
okAndMore.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
String name = nameBox.getText();
if (name.length() > 0) {
items.add(name);
statusLbl.setText(name + " added");
}
nameBox.setText("");
nameBox.setFocus(true);
}
});
I have a rather large form, that I've broken up into individual sections with "Next" and "Back" buttons for the user to navigate each step.
Here is how I set it up to work for now:
Each section is within its own xp:panel
Each xp:panel is hidden with "display:none". The transition happens when the user clicks on "Next" or "Back" by using JQuery's fade animation
What I am trying to do is, if the user clicks on "Next" I would like to validate only the fields in the current, visible section. If the validation fails, don't transition to the next step. If the validation passes, transition to the next step.
Right now, when I click on the "Next" button, every field is being validated and the transition doesn't happen.
Is there a way, that I can only validate the fields in a certain section or would I have to use something like Don Mottolo's code snippet: http://openntf.org/XSnippets.nsf/snippet.xsp?id=ssjs-form-validation-that-triggers-errormessage-controls
Thank you for your help!
P.S.: I know I could use the CSJS portion of the onClick event of the button to run some validation, but I'd like to use the "Display Error" controls.
You could look at computing the required attribute of each control that is to be validated and have that check which button is submitting the form. Tommy Vaaland has a function that does this:
// Used to check which if a component triggered an update
function submittedBy( componentId ){
try {
var eventHandlerClientId = param.get( '$$xspsubmitid' );
var eventHandlerId = #RightBack( eventHandlerClientId, ':' );
var eventHandler = getComponent( eventHandlerId );
if( !eventHandler ){ return false; }
var parentComponent = eventHandler.getParent();
if( !parentComponent ){ return false; }
return ( parentComponent.getId() === componentId );
} catch( e ){ /*Debug.logException( e );*/ }
}
Link: http://dontpanic82.blogspot.co.uk/2010/03/xpages-making-validation-behave.html
You can look at using a client side form validation framework such as Parsley: http://parsleyjs.org
This can of course be combined with server side validation for at least the final submission.
The purpose of the following function is to allow the user to save the edited page properties in the CQ-Dialog even though if they invalid by clicking on the button save anyway:
PageProperties.showMsg = function(dialog, config, errorMessage) {
CQ.Ext.MessageBox.buttonText.ok = "save anyway";
CQ.Ext.Msg.show({
title : "Completeness check failed",
msg : errorMessage,
buttons: CQ.Ext.Msg.OKCANCEL,
fn : function(buttons) {
if(buttons == "ok") {
dialog.form.items.each(function(field) {
// clear fields with emptyText so emptyText is not submitted
if (field.emptyText && field.el && field.el.dom && field.el.dom.value == field.emptyText) {
field.setRawValue("");
}
});
var action = new CQ.form.SlingSubmitAction(dialog.form, config);
dialog.form.isValid = function() {
return true;
};
dialog.form.doAction(action);
dialog[dialog.closeAction]();
CQ.Util.reload();
}
}
});
};
This functions works fine from the sidekick. When I click on save anyway all current values of the page properites are stored regardless if they are valid or not. This does not work from the site admin. when I call the page properties of the same page from the site admin and try to save the page properties with invalid values by clicking on save anyway, this does not works (old values are stored and nothing changes).
I hope somebody can help. thank you
I found the solution. the problem was the function CQ.Util.reload(). It prevent storing the values
I have a search form and a login form on my website. When the enter button is pressed when the login form has focus, the search runs instead of the login. Is there a way to fix this?
I've already tried using a panel around the login form and use defaultbutton, but the loginview errors when I do this.
You could try setting up a keypress event on your login form. Off the top of my head, something like
$('#loginForm').keypress(function (e) {
if(e.keyCode=='13') //Keycode for "Return"
$('#login').click();
}
});
should work, assuming you give appropriate IDs to the elements involved.
If you're talking about HTML, then this suggests tab order (learn more about that here) is relevant or the order in which the forms were created, the behavior depending on the user agent.
could you try adding attributes at run time like this
Login loginControl = (Login)lvLoginView.FindControl("logLogin");
TextBox tbUserName = (TextBox)loginControl.FindControl("UserName");
TextBox tbPassword = (TextBox)loginControl.FindControl("Password");
Button loginButton = (Button)loginControl.FindControl("LoginButton");
tbUserName.Attributes["onKeyPress"] = "KeyDownHandler('" + loginButton.ClientID + "')";
tbPassword.Attributes["onKeyPress"] = "KeyDownHandler('" + loginButton.ClientID + "')";
and some JS:
function KeyDownHandler(btn){
// process only the Enter key
if (event.keyCode == 13) {
// cancel the default submit
event.returnValue = false;
event.cancel = true;
var obj = document.getElementById(btn);
obj.click();
}}
UPDATE
Auto-converted to VB.NET courtesy of telerik
Dim loginControl As Login = DirectCast(lvLoginView.FindControl("logLogin"), Login)
Dim tbUserName As TextBox = DirectCast(loginControl.FindControl("UserName"), TextBox)
Dim tbPassword As TextBox = DirectCast(loginControl.FindControl("Password"), TextBox)
Dim loginButton As Button = DirectCast(loginControl.FindControl("LoginButton"), Button)
tbUserName.Attributes("onKeyPress") = "KeyDownHandler('" + loginButton.ClientID + "')"
tbPassword.Attributes("onKeyPress") = "KeyDownHandler('" + loginButton.ClientID + "')"