Link to home
Start Free TrialLog in
Avatar of darby
darby

asked on

Checking a number is entered

Is there any easy way of checking that a number is entered rather than a letter ??

I get string error if someone enters a letter where a number should be...


Thanks


dean..
Avatar of fritz_the_blank
fritz_the_blank
Flag of United States of America image

You can do client-side validation of the field as follows:

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>

<SCRIPT LANGUAGE=javascript>
<!--
function NumberValidation()
{
strFieldValue = document.form1.text1.value
if(isNaN(strFieldValue))
     {
     alert(strFieldValue + " is not a number!");
     }
}
//-->
</SCRIPT>

</HEAD>
<BODY>

<FORM action="" method=POST id=form1 name=form1><P>&nbsp;</P>
Test Input<INPUT type="text" id=text1 name=text1 >
<INPUT type="button" value="Button" id=button1 name=button1 onClick="JavaScript:NumberValidation()">
</FORM>
</BODY>
</HTML>
Use below JavaScript function to test if the field is numeric.

<script language="JavaScript">
<!--
// check if string is numeric
function IsNumeric(string)
{
     var i;
     var nLength = string.length;
     var valid = true;
     
     for ( i=0; i<nLength; i++ )
     {
          if ( string.charAt(i) >= '0' && string.charAt(i) <= '9' )
               continue;
          else
          {
               valid = false;
               break;
          }
     }
     
     return valid;
}

function validate_form(form)
{
    if ( !IsNumeric(form.txtAge.value) )
    {
        alert("Please enter an integer for age");
        return false;
    }
    else
        return true;
}
//-->
</script>


...

<form name="frmMain" method="post" action="second_page.asp" onsubmit="return validate_form(this)">
Age: <input type="text" name="txtAge">
<br>
<input type="submit">
</form>


hongjun
Just another approach.

hongjun
ASKER CERTIFIED SOLUTION
Avatar of dannic
dannic

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hey DanniC,

Please read the information regarding the distinction between comments and answers below and on the link when you submit a post. Once you propose an answer, experts who haven't posted to this thread will be unable to participate.

Most experts post as comments even when they are certain that they are answering the question. The person posing the question can accept a comment as an answer at any time.

Fritz the Blank
Avatar of dannic
dannic

My apologize to fritz_the_blank, hongjun and darby.
Avatar of darby

ASKER

You know why :)
Avatar of darby

ASKER

Ok Fritz.

Your solutions works on a single input, but i think i might be making it a little harder.

the form is generated by a database, and creates multiple input boxes with the title  'quantity' when i try to get it to work with multiple boxes it gives an error.

So i guess i will have to try Dannic's server side - unless you know another way :)



Dean..
Hey Dean,

It may still be an easy thing using client-side JavaScript. Do all of the fields have similar names, i.e. quantity1, quantity2 and etc? If so, you can do something like this:

for (var i=0;i<document.form1.elements.length;i++)
     if (document.form1.elements[ i ].name.substring(0,7) == "quantity")
     {
     //do the error checking here
     }
     alert("Your Error Message");    
 }

If you want to give me the field names as dynamically generated by your script, I'll modify this to work for you.

Fritz the Blank
Avatar of darby

ASKER

Unfortunately they all have the same name, they are then put into an array i.e. 4 x boxes all called Quantity, generate an array of 1,2,3,4 etc. when passed to the next page.



Dean..
Avatar of darby

ASKER

Thanks Dannic

Your solution suited they way i'd set my form up !!


Dean.
Darby,

Were any of these comments helpful? If so, please select one as an answer.

Fritz the Blank
Avatar of darby

ASKER

Sorry

I'm sure i accepted it before, not just commented it...