Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Web Design, HTML And CSS Forums (http://www.go4expert.com/forums/web-design-forum/)
-   -   Need help for Numeric validation of text fields (http://www.go4expert.com/forums/help-numeric-validation-text-fields-t17972/)

saturn 13Jun2009 16:58

Need help for Numeric validation of text fields
 
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

SaswatPadhi 13Jun2009 18:41

Re: Need help for Numeric validation of text fields
 
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 ! :)

saturn 15Jun2009 11:09

Re: Need help for Numeric validation of text fields
 
It worked nicely...thank u vry much...i nearly cracked my head for this !!! :)

SaswatPadhi 15Jun2009 12:06

Re: Need help for Numeric validation of text fields
 
My pleasure !! :pleased:

itsmeka 7Dec2009 01:13

Re: Need help for Numeric validation of text fields
 
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


All times are GMT +5.5. The time now is 02:40.