Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate numeric data vs. alpha

Status
Not open for further replies.

Chopstik

Technical User
Oct 24, 2001
2,180
US
I've created a script that will test if the user enters numeric data (or any data at all for that matter). It works fine except for one thing - if the user enters "2d", it still reads it as true and doesn't recognise the alpha character. Can someone look at my code and tell me what I'm doing wrong? I'm just trying to beat my users to the punch (before they start doing things they're not supposed to). Thanks. My code is below:

<SCRIPT type=&quot;text/JavaScript&quot;>
function Validate()
{
var Tax;
var Freight;

Tax = parseFloat(frmPOInfo.strTax.value);
Freight = parseFloat(frmPOInfo.strFreightAmt.value);

if (isNaN(Tax) && frmPOInfo.strTax.value.length != 0)
{
alert(&quot;You must enter numeric data for Tax amount.&quot;);
return false
}
else if (isNaN(Freight) && frmPOInfo.strFreight.value.length != 0)
{
alert(&quot;You must enter numeric data for Freight amount.&quot;);
return false
}
}
</SCRIPT>
 
Try this:
function alpha(value){//for example text.value
var re = /[0-9]/ //any digit?
if ((value.search(re)!= -1)){//found any digit at any position
..your code on error
}
else
..no digits found .ok}
 
fayna,

I tried your code, but it doesn't do what I need. I need to tell the users they are entering in non-numeric data. Your code (and my mind is a little scrambled at the moment b/c I tried a couple of different variations) only pops an error if they enter both alpha and numeric data. If they enter only alpha data, there is no error. Not sure if there is another way around this... Thanks, though...
 
Never mind, I made another slight adjustment and finally got it to work. Here's the code in case you're interested. Thanks!

<SCRIPT type=&quot;text/JavaScript&quot;>
function Validate()
//This should test whether the entry is numeric or alpha. If alpha, will alert user.
//6/12/02 - if user first enters numeric and then alpha, does not appear to work. Under research.
{
var Tax;
var Freight;
var re = /[^0-9]/;

Tax = parseFloat(frmPOInfo.strTax.value);
Freight = parseFloat(frmPOInfo.strFreightAmt.value);

if((isNaN(Tax) && frmPOInfo.strTax.value.length != 0) || (frmPOInfo.strTax.value.search(re) != -1))
{
alert(&quot;You must enter numeric data for Tax amount.&quot;);
document.frmPOInfo.strTax.focus();
document.frmPOInfo.strTax.select();
return false
}
else if((isNaN(Freight) && frmPOInfo.strFreightAmt.value.length != 0) || (frmPOInfo.strFreightAmt.value.search(re) != -1))
{
alert(&quot;You must enter numeric data for Freight amount.&quot;);
document.frmPOInfo.strFreightAmt.focus();
document.frmPOInfo.strFreightAmt.select();
return false
}
}
</SCRIPT>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top