cdemott33
asked on
VB Script IsNumeric Function and the dollar sign
Hi - I have a classic ASP page that uses VBScript and I need some help.
I have a form that allows a user to enter a budget amount. I want to insure the value the user enter EXCLUDES the dollar sign ($) and the comma (,).
Example:
$10,500.00 (False)
10500.00 (True)
I wrote this to test the value entered...
If NOT IsNumeric(strBudget) THEN
' message
End If
.... but IsNumeric allows the dollar sign and comma and returns True.
Is there another function that I could use to test the value entered? I want to make sure the user isn't entering a dollar sign or a comma.
Thanks.
I have a form that allows a user to enter a budget amount. I want to insure the value the user enter EXCLUDES the dollar sign ($) and the comma (,).
Example:
$10,500.00 (False)
10500.00 (True)
I wrote this to test the value entered...
If NOT IsNumeric(strBudget) THEN
' message
End If
.... but IsNumeric allows the dollar sign and comma and returns True.
Is there another function that I could use to test the value entered? I want to make sure the user isn't entering a dollar sign or a comma.
Thanks.
You may want to use Regular Expressions, and test on this pattern:^\d\.?\d*$
You will have to check for the existence of the "$" and "," on top of the IsNumeric check. Use the InStr function to do this: http://www.w3schools.com/VBscript/func_instr.asp
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
matthewspatrick - Can you provide an example of how I could do this?
If you could use javascript -
script would be this -
function validateNumberField (objEvent)
{
var iKeyCode;
iKeyCode = objEvent.charCode ? objEvent.charCode : objEvent.keyCode;
if(iKeyCode>=48 && iKeyCode<=57) return true;
if (iKeyCode == 8) return true;
return false;
}
then your code would have something like this:
<input type="text" size=1 name="month" id="month" onKeyPress="return validateNumberField(event) ;">
and only allow numbers.
script would be this -
function validateNumberField (objEvent)
{
var iKeyCode;
iKeyCode = objEvent.charCode ? objEvent.charCode : objEvent.keyCode;
if(iKeyCode>=48 && iKeyCode<=57) return true;
if (iKeyCode == 8) return true;
return false;
}
then your code would have something like this:
<input type="text" size=1 name="month" id="month" onKeyPress="return validateNumberField(event)
and only allow numbers.
>>You will have to check for the existence of the "$" and "," on top of the IsNumeric checkAnd that's not all. For example, these return true as well:IsNumeric(" 1234")IsNumeric("1234 ")IsNumeric(" 1234 ")RegExp is looking better and better :)
For an example, see:https://www.experts-exchange.com/questions/21591244/Phone-number-validation-using-a-regular-expression.html that you should use the pattern string I supplied in my first comment...
ASKER
Thank you. Using a regexp match worked perfectly.