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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Aug 2013
    Posts
    142
    Thanks
    22
    Thanked 0 Times in 0 Posts

    problems with functions

    Firstly I have the following function for date validation and it works fine but the form still gets submitted even though I am using return false.

    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))  
    	{  
    		document.form.dateRec.focus();  
    		//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.dateRec.focus();  
    		return false;  
    	}  
    }
    Secondly I have a function to validate names but for some reason it is not being called anyway here is the function and also the method I am using to call it. Any suggestions would be greatly appreciated thanks:

    Code:
    function check(form) {
    document.getElementById("message").innerHTML = "";
    var fn = form.fName;
    var fnv = form.fName.value;
    fnv = fnv.replace(/\s+/g,"");  // strip all spaces OR
    //fnv = fnv.replace(/^\s+|\s+$/g,"");  // strip only leading and trailing spaces;
    //fnv = fnv.replace(/\s{2,}/g," ");  // Replace multiple spaces with one space
    // convert whatever is entered into lower case but with a capitalised first letter
    fnv = fnv.toLowerCase().replace(/\b[a-z]/g,function(w){return w.toUpperCase()});
    form.fName.value = fnv;  // write it back to the field
    if ((fnv == "Firstname") || (fnv.length == 0) || (fnv.length < 2) || (/[^a-z\-\']/gi.test(fnv))) {  // only a-z hyphen and apostophe allowed in proper name
    form.fName.value= "";  // clear the invalid field
    var msg = "Invalid - not enouogh characters in forename!";
    document.getElementById("message").innerHTML = msg;
    myfield = fn;  // note myfield must be a global variable
    setTimeout('myfield.focus();myfield.select();' , 10);  // to overcome bug in Firefox
    return false;
    }
    
    var ln = form.sName;
    var lnv = form.sName.value;
    lnv = lnv.replace(/\s+/g,"");  // strip all spaces OR
    //lnv = lnv.replace(/^\s+|\s+$/g,"");  // strip only leading and trailing spaces;
    //lnv = lnv.replace(/\s{2,}/g," ");  // Replace multiple spaces with one space
    // convert whatever is entered into lower case but with a capitalised first letter
    lnv = lnv.toLowerCase().replace(/\b[a-z]/g,function(w){return w.toUpperCase()});
    form.sName.value = lnv;  // write it back to the field
    if ((lnv == "Lastname") || (lnv.length == 0) ||(lnv.length < 3) || (/[^a-z\-\']/gi.test(lnv))) {  // only a-z hyphen and apostophe allowed in proper name
    form.sName.value= "";  // clear the invalid field
    var msg = "Invalid - not enough charatcers in surname!";
    document.getElementById("message").innerHTML = msg;
    myfield = ln;  // note myfield must be a global variable
    setTimeout('myfield.focus();myfield.select();' , 10);  // to overcome bug in Firefox
    return false;
    }
    return true;
    }
    Method I am using to call the function:

    Code:
    function clickEvent()
    {
    	return valDate(document.form.dateRec);
    	return check(form);
    	checkEmail();
    }
    At this point I would also like to point out that the checkEmail() function gets called ok and as you know the valDate() function gets called ok.

  • #2
    New Coder
    Join Date
    May 2005
    Location
    New Zealand
    Posts
    76
    Thanks
    0
    Thanked 6 Times in 6 Posts
    The reason why the check function is never called is that in the clickEvent() function you return on the line that calls the valDate function so the code after it never gets executed because the program execution has left the clickEvent function.

    You need to modify the click Event function to something like the following...
    Code:
    function clickEvent()
    {
       if ((!valDate(etc)) || (!check(etc)) || (!checkEmail()))
          return false;
    }
    Basically if any of the check functions return false, then return false from the clickEvent function so that the form does not submit.

  • #3
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by daniel0816 View Post
    Firstly I have the following function for date validation and it works fine but the form still gets submitted even though I am using return false.
    That suggests that the return value isn't being read by the onsubmit handler.
    How are you installing the clickEvent function?

    If you've done something like onsubmit="clickEvent()", change it to onsubmit="return clickEvent()"
    Last edited by Logic Ali; 10-07-2013 at 01:49 AM.

  • #4
    Regular Coder
    Join Date
    Aug 2013
    Posts
    142
    Thanks
    22
    Thanked 0 Times in 0 Posts
    Still no joy the date function gets called but form still submits and email function works fine and check function doesn't get called at all. Here is where I am calling the clickEvent() function:

    Thanks Again, appreciate it

    Code:
    <div style="width:30%; margin-top:-14px;" ><input type="submit" name="submit" value="Submit" onclick="clickEvent();" /></div>

  • #5
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by daniel0816 View Post
    Still no joy the date function gets called but form still submits and email function works fine and check function doesn't get called at all. Here is where I am calling the clickEvent() function:
    Well you've ignored my advice: onclick="return clickEvent();"

  • #6
    Regular Coder
    Join Date
    Aug 2013
    Posts
    142
    Thanks
    22
    Thanked 0 Times in 0 Posts
    Ah sorry yea that kind of worked. The date validation function now works fine, the form no longer gets submitted, but now the other two functions aren't being called at all.

    Code:
    function clickEvent()
    {
    	if((!valDate(document.form.dateRec)) || (!check(this)) || (!checkEmail()))
    	{
    		return false;
    	}
    }
    Code:
    <div style="width:30%; margin-top:-14px;" ><input type="submit" name="submit" value="Submit" onclick="return clickEvent();" /></div>

  • #7
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Your check() function has an unconditional return false, which prevents calling of a later function.

    What is the code appearing below the check function? It looks like open code but includes a return.

    When you call check you are passing it this, which when called by the submit button will not reference the form.

    clickEvent would be easier to understand written like this:

    Code:
    function clickEvent( theForm )
    {
       return valDate(document.form.dateRec) && check( theForm ) && !checkEmail();
    }
    Must be called thus:
    Code:
     onclick="return clickEvent( this.form )"

  • Users who have thanked Logic Ali for this post:

    daniel0816 (10-07-2013)


  •  

    Posting Permissions

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