1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Need help for Numeric validation of text fields

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

  1. saturn

    saturn New Member

    Joined:
    Jun 2, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    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δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    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

    Joined:
    Jun 2, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    It worked nicely...thank u vry much...i nearly cracked my head for this !!! :)
     
  4. SaswatPadhi

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

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    My pleasure !! :pleased:
     
  5. itsmeka

    itsmeka New Member

    Joined:
    Dec 6, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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