We help IT Professionals succeed at work.

Checking a number is entered

darby
darby asked
on
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..
Comment
Watch Question

BRONZE EXPERT
Top Expert 2005

Commented:
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>
BRONZE EXPERT

Commented:
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
BRONZE EXPERT

Commented:
Just another approach.

hongjun
Commented:
You should always check the information on serverside as well as on the client side.

So you can use a script like the ones above and on the server side you can do the check with The IsNummeric function, it returns True or False.
eg.
if IsNumeric(Request.Form("text1")) then
 'do some stuff
else
 'do some other stuff
end if

It is good to check as much as possible at the client to reduse the network traffic. The check on the serverside is used to avoid that errors occurs if someone has been able to post "incorrect"  information to the server.
BRONZE EXPERT
Top Expert 2005

Commented:
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

Commented:
My apologize to fritz_the_blank, hongjun and darby.

Author

Commented:
You know why :)

Author

Commented:
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..
BRONZE EXPERT
Top Expert 2005

Commented:
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

Author

Commented:
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..

Author

Commented:
Thanks Dannic

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


Dean.
BRONZE EXPERT
Top Expert 2005

Commented:
Darby,

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

Fritz the Blank

Author

Commented:
Sorry

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

Explore More ContentExplore courses, solutions, and other research materials related to this topic.