form validation not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • obtrs
    New Member
    • May 2009
    • 27

    form validation not working

    im using java script for form validation..

    here is the code

    Code:
    <script type="text/JavaScript">
    
     
     function validate_required(field,alerttxt)
    {
    with (field)
      {
      if (value==null||value=="")
        {
        alert(alerttxt);return false;
        }
      else
        {
        return true;
        }
      }
    }
    
    function validate_form(thisform)
    {
    with (thisform)
      {
      if (validate_required(email,"Email must be filled out!")==false)
      {email.focus();return false;}
    
      }
     
    }
    
     
     
     function valid(f) {
    if (!/^\d*$/.test(f.value)) {
    alert("Only integer numbers allowed!");
    f.value = f.value.replace(/[^\d]/g,"");
    }
    }
     
     
     </script>

    The script working fine with email validation and also the last one function is for contact no input field only takes numbers every thing is working fi9 now i want the code to work like if name field is empty it says fill name and so on for all input fields.

    HTML FORM:
    Code:
    div id="forminfo"><form name="theform" action="<?php echo $_SERVER['PHP_SELF']; ?>" onsubmit="return validate_form(this)" method="POST">
        First Name:
        *************<INPUT type="text" name=fname style="HEIGHT: 19px; WIDTH: 174px" size="20" align="center">
        <br>
        <br>
        
        Last Name:
       *************<INPUT type="text" name=lastname style="HEIGHT: 19px; WIDTH: 174px" size="20" align="center">
        <br>
        <br>
        
        Email Address:*******<INPUT type="text" name=email style="HEIGHT: 19px; WIDTH: 174px" size="20" align="center">
        <br>
        <br>
        
        Address:
        ******************<INPUT type="text" name=address style="HEIGHT: 19px; WIDTH: 174px" size="20" align="center">
        <br>
        <br>
        
        City:
            **************************<INPUT type="text" name=city style="HEIGHT: 19px; WIDTH: 174px" size="20" align="center">
        <br>
        <br>
        
        Province:
        *****************<INPUT type="text" name=province style="HEIGHT: 19px; WIDTH: 174px" size="20" align="center">
        <br>
        <br>
        
        Contact No:
        ************<INPUT type="text" name=contactno maxlength="11"  onkeyup="valid(this)" style="HEIGHT: 19px; WIDTH: 174px" size="20" align="center">
        <br>
        <br>
        <br>
           ***********************************************************************************<input type="submit" name="submit" value="submit" />
        
        </form>
  • prabirchoudhury
    New Member
    • May 2009
    • 162

    #2
    form validation not working Reply to Thread

    Hey
    this should work fine as form validation

    Html form
    Code:
    <div id="forminfo">
    	<form name="theform" action="<?php echo $_SERVER['PHP_SELF']; ?>" onsubmit="return verify()" method="POST"> 
        First Name: <INPUT type="text" name=fname style="HEIGHT: 19px; WIDTH: 174px" size="20" align="center"> 
        <br> 
        <br> 
      
        Last Name: <INPUT type="text" name=lastname style="HEIGHT: 19px; WIDTH: 174px" size="20" align="center"> 
        <br> 
        <br> 
      
        Email Address:<INPUT type="text" name=email style="HEIGHT: 19px; WIDTH: 174px" size="20" align="center"> 
        <br> 
        <br> 
      
        Address:<INPUT type="text" name=address style="HEIGHT: 19px; WIDTH: 174px" size="20" align="center"> 
        <br> 
        <br> 
      
        City:<INPUT type="text" name=city style="HEIGHT: 19px; WIDTH: 174px" size="20" align="center"> 
        <br> 
        <br> 
      
        Province:<INPUT type="text" name=province style="HEIGHT: 19px; WIDTH: 174px" size="20" align="center"> 
        <br> 
        <br> 
      
        Contact No:<INPUT type="text" name=contactno maxlength="11"  onkeyup="valid(this)" style="HEIGHT: 19px; WIDTH: 174px" size="20" align="center"> 
        <br> 
        <br> 
        <br> <input type="submit" name="submit" value="submit" /> 
      
        </form> 
    </div>

    Java script

    Code:
    <script type="text/JavaScript">
     function verify(){
    
        var digits = "0123456789";
    	  var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i ;
    	  var numericExpression = /^[0-9]+$/;
    	  var str=document.theform.email.value;
            if(document.theform.fname.value==""){
    		    alert("Please enter fname");
    		    document.theform.fname.focus();
            return false;
            }else
            if(document.theform.lastname.value==""){
    		    alert("Please enter lastname");
    		    document.theform.lastname.focus();
            return false;
            }else
            if(!filter.test(str)){
    		    alert("Please input a valid email address");
    		   document.theform.email.focus();
            return false;
            }else
            if(document.theform.address.value==""){
    		    alert("Please input a valid  address");
    		   document.theform.address.focus();
            return false;
            }else
            if(document.theform.city.value==""){
    		    alert("Please enter city");
    		   document.theform.city.focus();
            return false;
            }else
            if(document.theform.province.value==""){
    		    alert("Please enter province");
    		   document.theform.province.focus();
            return false;
            }else
            if(document.theform.contactno.value==""){
    		    alert("Please enter contact no");
    		   document.theform.contactno.focus();
            return false;
            }else
            if(!(document.theform.contactno.value.match(numericExpression))){
    		    alert("Please enter number only\n contact no");
    		   document.theform.contactno.focus();
            return false;
            }
    	  
    } 
     </script>

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5390

      #3
      first: wrap all your name-attribute values in double quotes ...
      second: just retrieve the input fields with

      Code:
      var inpuptFields = document.getElementsByTagName('input');
      then loop over the retrieved list and implement the required validation ... in case you have problems with this just show what you have tried so far regarding this problem

      kind regards

      Comment

      Working...