Page 1 of 1 [ 5 posts ] 

SDerailed
Tufted Titmouse
Tufted Titmouse

User avatar

Joined: 29 Jan 2015
Age: 42
Gender: Female
Posts: 31

05 Feb 2015, 2:18 pm

I need a little advice about form validation. I need to validate a drop down select box, a radio button, and a checkbox input button before submit. How can I validate these without a huge loopy mess? Any suggestions?



0_equals_true
Veteran
Veteran

User avatar

Joined: 5 Apr 2007
Age: 42
Gender: Male
Posts: 11,038
Location: London

06 Feb 2015, 4:12 pm

SDerailed wrote:
I need a little advice about form validation. I need to validate a drop down select box, a radio button, and a checkbox input button before submit. How can I validate these without a huge loopy mess? Any suggestions?

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
Code:
document.getElementById('yourFormID')


You can do
Code:
document.getElementById('yourForm').fieldName.value


By it is also good practice to provide id for you form elements

Code:
document.getElementById('fieldID').value


Are you using jQuery?
Code:
jQuery('form#yourForm #fieldID').val()


I don't know what you mean 'loopy mess' you can just use normal logic like if statements

Create a callback

Code:
function submitCallback(){
   
    // 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


Code:
document.getElementById('fieldID').onsubmit = submitCallback;



More modern method is

Code:
var frm = document.getElementById('yourForm');

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

Code:
jQuery('form#yourForm #fieldID').submit(submitCallback);




That handles all the event binding quirkiness cross browser.



SDerailed
Tufted Titmouse
Tufted Titmouse

User avatar

Joined: 29 Jan 2015
Age: 42
Gender: Female
Posts: 31

08 Feb 2015, 12:40 am

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;
}



0_equals_true
Veteran
Veteran

User avatar

Joined: 5 Apr 2007
Age: 42
Gender: Male
Posts: 11,038
Location: London

08 Feb 2015, 5:59 pm

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.

Code:

// 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;
}



SDerailed
Tufted Titmouse
Tufted Titmouse

User avatar

Joined: 29 Jan 2015
Age: 42
Gender: Female
Posts: 31

08 Feb 2015, 11:33 pm

Perfect. Thank you.
I am taking an intro to Javascript class, and we aren't very far along. But after I finished this project for the chapter, it left me thinking that there had to be a more efficient way to do this.