Results 1 to 3 of 3

Thread: Form Validating - Return/Submit issue?

  1. #1
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default Form Validating - Return/Submit issue?

    I've written a validation script to validate forms before submission. One form is a subscription form (requiring only name and email), while the other is a full-fledged contact form. The script works for both the name and email of the mailing list form, but still submits the form after clicking ok on both alerts. The contact form, on the other hand, throws the alert for the first field (name), then submits the form without going through the other fields.

    Below is the javascript, then the two form lines where I call the functions. Help!

    Code:
    function validEmail(fieldName){
    	var validate = fieldName.value;
    	if(validate == null || validate == ''){
    		var apos = validate.indexOf("@");
    		var dotpos = validate.lastIndexOf(".");
    		if (apos < 1|| dotpos - apos < 2){
    			alert('Please enter a valid email address!');
    			return false;
    		}
    		else{
    			return true;
    		}
    	}
    }
    
    function validForm(fieldName2,fieldLoc,fieldType){
    	var validate = fieldLoc.value;
    	if(validate == null || validate == ''){
    		alert('Please enter a valid ' + fieldName2);
    		return false;
    	}
    	else{
    		if(fieldType == 'number'){
    			var validateNum = validate.replace(/[\(\)\.\-\ ]/g, '');
    			if(isNaN(parseInt(validateNum))){
    				alert('Please enter a valid ' + fieldName2);
    				return false;
    			}
    			else{
    				return true;
    			}
    		}
    		else if(fieldType == 'email'){
    			validEmail(fieldLoc);
    		}
    		else{
    			return true;
    		}
    	}
    }
    
    function validFormRun(){
    	validForm('name',this.name,'text');
    	validForm('address',this.address,'text');
    	validForm('city',this.city,'text');
    	validForm('message',this.message,'text');
    	validEmail(this.email);
    	validForm('phone number',this.phone,'number');
    	validForm('zip code',this.zip,'number');
    }
    Mailing list form:
    Code:
    <form action="forms.php" method="post" onsubmit="validForm('email address',this.mailing_email,'email'); validForm('cell phone number',this.mailing_cell,'number');">
    Contact form:
    Code:
    <form method="post" action="forms.php" id="contactForm" name="contactForm" onsubmit="return validFormRun()">
    Thanks a lot!
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  2. #2
    Join Date
    Nov 2008
    Posts
    58
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default

    The onsubmit of the mailing list should return false to stop the form from submitting.


    Code:
    <form action="forms.php" method="post" onsubmit="return validMailListForm()">
    The validFormRun() function should return true or false.

    like
    Code:
    function validFormRun(){
    	if(validForm('name',document.formname.name,'text')&&
    	 validForm('address',document.formname.address,'text') &&
    	validForm('city',document.formname.city,'text'))
    	{
    		  return true;
    	}
    	else
    	{
    		return false;
    	}
    }
    The Javascript validation code in the following page is very handy
    JavaScript Form Validation

  3. #3
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Still isn't working:

    Code:
    function validEmail(fieldName){
    	var validate = fieldName.value;
    	if(validate == null || validate == ''){
    		var apos = validate.indexOf("@");
    		var dotpos = validate.lastIndexOf(".");
    		if (apos < 1|| dotpos - apos < 2){
    			alert('Please enter a valid email address!');
    			return false;
    		}
    		else{
    			return true;
    		}
    	}
    }
    
    function validForm(fieldName2,fieldLoc,fieldType){
    	var validate = fieldLoc.value;
    	if(validate == null || validate == ''){
    		alert('Please enter a valid ' + fieldName2);
    		return false;
    	}
    	else{
    		if(fieldType == 'number'){
    			var validateNum = validate.replace(/[\(\)\.\-\ ]/g, '');
    			if(isNaN(parseInt(validateNum))){
    				alert('Please enter a valid ' + fieldName2);
    				return false;
    			}
    			else{
    				return true;
    			}
    		}
    		else if(fieldType == 'email'){
    			validEmail(fieldLoc);
    		}
    		else{
    			return true;
    		}
    	}
    }
    
    function validMailList(){
    	if(
    		validForm('email address',this.mailing_email,'email') &&
    		validForm('cell phone number',this.mailing_cell,'number')
    	){
    		return true;
    	}
    	else{
    		return false;
    	}
    }
    
    function validFormRun(){
    	if(
    		validForm('name',this.name,'text') &&
    		validForm('address',this.address,'text') &&
    		validForm('city',this.city,'text') &&
    		validForm('message',this.message,'text') &&
    		validEmail(this.email) &&
    		validForm('phone number',this.phone,'number') &&
    		validForm('zip code',this.zip,'number')
    	){
    		return true;
    	}
    	else{
    		return false;
    	}
    }
    HTML Code:
    <form method="post" action="#" id="contactForm" name="contactForm" onsubmit="return validFormRun()">
    HTML Code:
    <form action="#" method="post" onsubmit="return validMailList()">
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •