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 3 of 3
  1. #1
    New Coder
    Join Date
    Oct 2006
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript Numerical Field Validation

    Hi there,

    I'm trying to validate a form field to only allow numerical values when the form is submitted. It appears to work ok but after it has detected a blank or invalid entry in the field and displayed the corresponding error message, it is still submitting the form. The 'return false;' statements do not appear to be working.....any ideas?

    Function script:
    Code:
    <script language="javascript">
    <!--
    var error = "";
    var submitted = 0;
    
    function Validator(form1)
    {
    var digits = "0123456789";
    
    if (submitted) 
    {
    alert("Form already submitted, please wait");
    return false;
    }
    
      if (form1.jobref.value == "")
      {
        error += "Please enter a Job Reference number.\n";
      }
     	 for (var i = 0; i < form1.jobref.value.length; i++)
      		{
        	temp = form1.jobref.value.substring(i, i+1)
    
        		if (digits.indexOf(temp) == -1 && form1.jobref.value != "")
        		{
          		error += "The Job Reference number you entered is invalid.\n";
          		break;
        		}
      		} 
    
    	if (error != "")
      	{
        alert(error);
        return false;
      	} 
    		else if (!submitted)
    		{
    		submitted = 1;
    		return true;
      		} 
    }
    
    -->
    </script>
    Form Code:
    Code:
    <form id="form1" name="form1" method="post" action="ITJobReport.asp">
    
    <input name="jobref" type="text" id="jobref" />
    
    <input type="submit" name="Submit" value="Submit" style="width:130px" onclick="Validator(form1)"/>

    I have also tried using the following, but it stops the form being submitted at all and just displays the "The Job Reference number you entered is invalid" message:
    Code:
    onclick="return Validator(form1)"
    Thanks

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,518
    Thanks
    3
    Thanked 506 Times in 493 Posts
    Code:
    <form id="form1" name="form1" onsubmit="return Validator(this);" method="post" action="ITJobReport.asp">
    
    <input name="jobref" type="text" id="jobref" />
    
    <input type="submit" name="Submit" value="Submit" style="width:130px" />
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    New Coder
    Join Date
    Oct 2006
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by vwphillips View Post
    Code:
    <form id="form1" name="form1" onsubmit="return Validator(this);" method="post" action="ITJobReport.asp">
    
    <input name="jobref" type="text" id="jobref" />
    
    <input type="submit" name="Submit" value="Submit" style="width:130px" />
    Ah I see what I did....d'oh.....thanks very much, worked perfectly


  •  

    Posting Permissions

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