Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Aug 2013
    Posts
    141
    Thanks
    22
    Thanked 0 Times in 0 Posts

    form validation functions are being called but form still gets submitted

    I have the following javascript form validation functions which are being called but the form still gets submitted:

    Code:
    function valDate(inputText)
    {
    	var dateformat = /^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/;  
    	// Match the date format through regular expression  
    	if(inputText.value.match(dateformat))  
    	{  
    		//Test which seperator is used '/' or '-'  
    		var opera1 = inputText.value.split('/');  
    		var opera2 = inputText.value.split('-');  
    		lopera1 = opera1.length;  
    		lopera2 = opera2.length;  
    		// Extract the string into month, date and year  
    		if (lopera1>1)  
    		{  
    			var pdate = inputText.value.split('/');  
    		} 
    		else if (lopera2>1)  
    		{  
    			var pdate = inputText.value.split('-');  
    		}  
    		var dd = parseInt(pdate[0]);  
    		var mm  = parseInt(pdate[1]);  
    		var yy = parseInt(pdate[2]);  
    		// Create list of days of a month [assume there is no leap year by default]  
    		var ListofDays = [31,28,31,30,31,30,31,31,30,31,30,31];  
    		if (mm==1 || mm>2)  
    		{  
    			if (dd>ListofDays[mm-1])  
    			{  
    				alert('Invalid date format!');  
    				return false;  
    			}  
    		}  
    		if (mm==2)  
    		{  
    			var lyear = false;  
    			if ( (!(yy % 4) && yy % 100) || !(yy % 400))   
    			{  
    				lyear = true;  
    			}  
    			if ((lyear==false) && (dd>=29))  
    			{  
    				alert('Invalid date format!');  
    				return false;  
    			}  
    			if ((lyear==true) && (dd>29))  
    			{  
    				alert('Invalid date format!');  
    				return false;  
    			}  
    		}  
    	}  
    	else  
    	{  
    		alert("Invalid date format!");  
    		document.form.dateRec2.focus();  
    		return false;  
    	}
    }
    
    function valDate1(inputText)
    {
    	var dateformat = /^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/;  
    	// Match the date format through regular expression  
    	if(inputText.value.match(dateformat))  
    	{  
    		//Test which seperator is used '/' or '-'  
    		var opera1 = inputText.value.split('/');  
    		var opera2 = inputText.value.split('-');  
    		lopera1 = opera1.length;  
    		lopera2 = opera2.length;  
    		// Extract the string into month, date and year  
    		if (lopera1>1)  
    		{  
    			var pdate = inputText.value.split('/');  
    		} 
    		else if (lopera2>1)  
    		{  
    			var pdate = inputText.value.split('-');  
    		}  
    		var dd = parseInt(pdate[0]);  
    		var mm  = parseInt(pdate[1]);  
    		var yy = parseInt(pdate[2]);  
    		// Create list of days of a month [assume there is no leap year by default]  
    		var ListofDays = [31,28,31,30,31,30,31,31,30,31,30,31];  
    		if (mm==1 || mm>2)  
    		{  
    			if (dd>ListofDays[mm-1])  
    			{  
    				alert('Invalid date format!');  
    				return false;  
    			}  
    		}  
    		if (mm==2)  
    		{  
    			var lyear = false;  
    			if ( (!(yy % 4) && yy % 100) || !(yy % 400))   
    			{  
    				lyear = true;  
    			}  
    			if ((lyear==false) && (dd>=29))  
    			{  
    				alert('Invalid date format!');  
    				return false;  
    			}  
    			if ((lyear==true) && (dd>29))  
    			{  
    				alert('Invalid date format!');  
    				return false;  
    			}  
    		}  
    	}  
    	else  
    	{  
    		alert("Invalid date format!");  
    		document.form.workcomm.focus();  
    		return false;  
    	}
    }
    
    function valDate2(inputText)
    {
    	var dateformat = /^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/;  
    	// Match the date format through regular expression  
    	if(inputText.value.match(dateformat))  
    	{  
    		//Test which seperator is used '/' or '-'  
    		var opera1 = inputText.value.split('/');  
    		var opera2 = inputText.value.split('-');  
    		lopera1 = opera1.length;  
    		lopera2 = opera2.length;  
    		// Extract the string into month, date and year  
    		if (lopera1>1)  
    		{  
    			var pdate = inputText.value.split('/');  
    		} 
    		else if (lopera2>1)  
    		{  
    			var pdate = inputText.value.split('-');  
    		}  
    		var dd = parseInt(pdate[0]);  
    		var mm  = parseInt(pdate[1]);  
    		var yy = parseInt(pdate[2]);  
    		// Create list of days of a month [assume there is no leap year by default]  
    		var ListofDays = [31,28,31,30,31,30,31,31,30,31,30,31];  
    		if (mm==1 || mm>2)  
    		{  
    			if (dd>ListofDays[mm-1])  
    			{  
    				alert('Invalid date format!');  
    				return false;  
    			}  
    		}  
    		if (mm==2)  
    		{  
    			var lyear = false;  
    			if ( (!(yy % 4) && yy % 100) || !(yy % 400))   
    			{  
    				lyear = true;  
    			}  
    			if ((lyear==false) && (dd>=29))  
    			{  
    				alert('Invalid date format!');  
    				return false;  
    			}  
    			if ((lyear==true) && (dd>29))  
    			{  
    				alert('Invalid date format!');  
    				return false;  
    			}  
    		}  
    	}  
    	else  
    	{  
    		alert("Invalid date format!");  
    		document.form.dateColl.focus();  
    		return false;  
    	}
    }
    
    function checkEmail() {
    
        var email = document.getElementById('eAdd2');
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    
        if (!filter.test(email.value)) {
        email.focus;
        return false;
     }
    }
    
    function checkEmail2() {
    
        var email = document.getElementById('compeAdd2');
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    
        if (!filter.test(email.value)) {
        email.focus;
        return false;
     }
    }
    
    function clickEvent( theForm )
    {
       return checkEmail() && checkEmail2();
    }
    Here is where the functions are being called:
    Code:
    <div style="width:33%; float:left; margin-top:-15px;" ><input type="submit" name="submit" value="Save Changes" onclick='clickEvent(this.form) & valDate(document.form.dateRec2) & valDate1(document.form.workcomm) & valDate2(document.form.dateColl);' /></div>
    Also when the email functions are called the form does not get submitted but when any of the date functions are called the form still gets submitted.
    Any help would be greatly appreciated thanks.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Code:
    <input type="submit" name="submit" value="Save Changes" onclick='return clickEvent(this.form) && valDate(document.form.dateRec2) && valDate1(document.form.workcomm) && valDate2(document.form.dateColl);' /></div>
    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Regular Coder
    Join Date
    Aug 2013
    Posts
    141
    Thanks
    22
    Thanked 0 Times in 0 Posts
    Sorry tried that, now nothing is called when I submit the form.

  • #4
    Regular Coder
    Join Date
    Aug 2013
    Posts
    141
    Thanks
    22
    Thanked 0 Times in 0 Posts
    I changed the && to just &, the functions are being called but the form still gets submitted anyway.


  •  

    Posting Permissions

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