Need help for Numeric validation of text fields

Light Poster
13Jun2009,16:58   #1
saturn's Avatar
Pls help me out !!!

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....!!!

Last edited by shabbir; 14Jun2009 at 09:00.. Reason: Code blocks
~ Б0ЯИ Τ0 С0δЭ ~
13Jun2009,18:41   #2
SaswatPadhi's Avatar
Change the script part to this :

HTML Code:
<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 !
Light Poster
15Jun2009,11:09   #3
saturn's Avatar
It worked nicely...thank u vry much...i nearly cracked my head for this !!!
~ Б0ЯИ Τ0 С0δЭ ~
15Jun2009,12:06   #4
SaswatPadhi's Avatar
My pleasure !!
Newbie Member
7Dec2009,01:13   #5
itsmeka's Avatar
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 Code:
<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 shabbir; 7Dec2009 at 09:19.. Reason: Code blocks