Any Javascript Coders out there?
As long as you realize that client side validation is complementary, not a fail-safe secure option.
Basically you haven't provided any info on how you want to validate.
To select an element by id
You can do
By it is also good practice to provide id for you form elements
Are you using jQuery?
I don't know what you mean 'loopy mess' you can just use normal logic like if statements
Create a callback
// validation logic
if(isValid){
return false;
}else{
return true;
}
}
You need to bind to the submit event. Writing even listener is a bit complex cross browser in hand-rolled javascrict.
The very old fashioned limited way is
More modern method is
if(frm.addEventListener){
frm.addEventListener("submit", submitCallback, false); //Modern browsers
}else if(frm.attachEvent){
frm.attachEvent('onsubmit', submitCallback); //Old IE
}
Note this doesn't cover all the event listener quirks of some of the ancient browsers.
In jQuery it is as simple as
That handles all the event binding quirkiness cross browser.
Thank you 0_equals_true. What I'm dealing with is not only text input boxes but other input types. So I'm not sure how to validate it efficiently. What I have now works, but the alerts pop up after every false. I'm hoping to have one function that lists all the invalid fields.
So here is the js for the form validation itself (I assume that you can infer as to what the HTML must be). You'll see that after the normal text input validation, it gets a bit different in scope.
function validate_form ( )
{
if( document.form.first_name.value == "" )
{
alert( "Please provide your first name!" );
document.form.first_name.focus() ;
return false;
}
if( document.form.last_name.value == "" )
{
alert( "Please provide your last name!" );
document.form.last_name.focus() ;
return false;
}
if( document.form.email.value == "" )
{
alert( "Please provide your email!" );
document.form.email.focus() ;
return false;
}
if( document.form.password.value == "" )
{
alert( "Please type a password!" );
document.form.password.focus() ;
return false;
}
if( document.form.confirmpassword.value == "" )
{
alert( "Please confirm your password!" );
document.form.confirmpassword.focus() ;
return false;
}
if(password.value != confirmpassword.value)
{
alert ("Your passwords must match.");
return false;
}
if ( ( document.form.sendemail[0].checked == false )
&& ( document.form.sendemail[1].checked == false ) )
{
alert ( "Please choose to have emails sent to you" );
return false;
}
if ( document.form.question.selectedIndex == 0 )
{
alert ( "Please select a security question." );
return false;
}
if( document.form.answer.value == "" )
{
alert( "Please provide a security answer!" );
document.form.answer.focus() ;
return false;
}
if ( ( document.form.interests[0].checked == false )
&& ( document.form.interests[1].checked == false )
&& ( document.form.interests[2].checked == false ))
{
alert ( "Please select a special interest." );
return false;
}
return true;
}
This is more an exercise in handling logic, I think you could benefit from learning JavaScript or any similar programing language more in depth. There is more than one to shell a peanut.
Fist alert boxes aren't necessarily he most user friendly feedback for a form, but we'll overlook that.
If you return from the function that is the as far as the logic goes. So instead you to collect the error messages then determine the result at the end.
// error collection
var errors = [];
if (!condition1){
errors.push("Condition 1 failed!");
}
if(!condition2){
errors.push("Condition 2 failed!");
}
if(!condition3){
errors.push("Condition 3 failed!");
}
// any errors?
if(errors.length > 0){
alert("The following error(s) occurred:\n" + errors.join("\n"));
return false;
} else {
return true;
}