1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Need help for Numeric validation of text fields

Discussion in 'Web Design, HTML And CSS Forums' started by saturn, Jun 13, 2009.

  1. saturn

    saturn New Member

    Pls help me out !!! :nonod:

    I have two text fields in a form that i need to validate to be non-empty and numeric(0-9) only.I have done the non-empty validation, but not able to do the numeric one.

    Here is my code-
    Code:
    <script language="JavaScript">
    function onlyNumbers()
    {
      valid=true;
      
      if (document.form1.consumerno.value == "")
      {
        alert ( "Please fill in your consumer no.." );
        valid = false;
      }
     if (document.form1.mobileno.value == "")
      {
        alert ( "Please fill in your mobile no.." );
        valid = false;
      }
     if (document.form1.consumerno.value == "ABCDEFGHIJKLMNOPQRSTUVWXYZ   abcdefghijklmnopqrstuvwxyz")
      {
        alert ( "Numbers only" );
        valid = false;
      }
      if (document.form1.mobileno.value == "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz")
      {
        alert ( "Numbers only" );
        valid = false;
      }
    return valid;
      }
    </script>
    
    And the form part as-
    Code:
    <form name="form1" action="success.html" method="post" onsubmit="return onlyNumbers();">
    <br><br>
    <center><b>Consumer Number:&nbsp;  
    <input type="text" name="consumerno">
    </b></center>
    <br><br>
    <center><b>Mobile Number:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    <input type="text" name="mobileno" >
    </b></center>
    <br><br><br>
    <center>
    <input type="submit" name="Submit" value="Submit"> &nbsp;&nbsp;&nbsp;&nbsp; 
    <input type="reset" name="Reset" value="Reset"></center>
    
    Can anybody help me out plssssssssss....!!! :disappoin
     
    Last edited by a moderator: Jun 14, 2009
  2. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Change the script part to this :

    HTML:
    <script language="JavaScript">
    	function onlyNumbers()
    	{
    		valid=true;
    		var Numbers = '0123456789';
    		var Field1 = document.form1.consumerno.value;
    		var Field2 = document.form1.mobileno.value;
    
    		if (Field1 == "")
    		{
    			alert ( "Please fill in your consumer no.." );
    			valid = false;
    		}
    		if (Field2 == "")
    		{
    			alert ( "Please fill in your mobile no.." );
    			valid = false;
    		}
    		for (i=0; i < Field1.length; i++)
    		{
    			if (Numbers.indexOf(Field1.charAt(i),0) == -1)
    			{
    				alert ( "Numbers only in Consumer Number." );
    				valid = false;
    				break;
    			}
    		}
    		for (i=0; i < Field2.length; i++)
    		{
    			if (Numbers.indexOf(Field2.charAt(i),0) == -1)
    			{
    				alert ( "Numbers only in Mobile Number." );
    				valid = false;
    				break;
    			}
    		}
    		return valid;
    	}
    </script>
    Enjoy ! :)
     
  3. saturn

    saturn New Member

    It worked nicely...thank u vry much...i nearly cracked my head for this !!! :)
     
  4. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    My pleasure !! :pleased:
     
  5. itsmeka

    itsmeka New Member

    actually im new here and in js. i want one more function there like the numeric value should be equal to 11digit as mobile number.
    i think that code should be like
    HTML:
    <script language="JavaScript">
    	function onlyNumbers()
    	{
    		valid=true;
    		var Numbers = '0123456789';
    		var Field1 = document.Form1.consumerno.value;
    		var Field2 = document.Form1.mobile.value;
    
    		if (Field1 == "")
    		{
    			alert ( "Please fill in your consumer no.." );
    			valid = false;
    		}
    		if (Field2 == "")
    		{
    			alert ( "Please fill in your mobile no.." );
    			valid = false;
    		}
    		for (i=0; i < Field1.length; i++)
    		{
    			if (Numbers.indexOf(Field1.charAt(i),0) == -1)
    			{
    				alert ( "Numbers only in Consumer Number." );
    				valid = false;
    				break;
    			}
    		}
    		for (i=0; i < Field2.length; i++)
    		{
    			if (Numbers.indexOf(Field2.charAt(i),0) == -1)
    			{
    				alert ( "Numbers only in Mobile Number." );
    				valid = false;
    				break;
    			}
    		}
    		return valid;
    	}
    	function validateForm(Form1)
     	{
    	  Form1.onsubmit = function(){
    		if(Form1.elements['mobile'].value.length<>11)
     		{
     			alert("You must put 11 digit!");
     			return false;
     		}
    
    
    		}
    </script>
    
    or what would be better
     
    Last edited by a moderator: Dec 7, 2009

Share This Page