|
-
Dec 4th, 2001, 03:07 AM
#1
Thread Starter
Frenzied Member
Function for validating a number
I have made a function for validating a text field. It is required that it is numeric. I have tried the code below, but the alert is activated when a "-" is entered. Can I use a IsNumeric method like in VB?
Code:
...
var err3 = 0;
var allowedchars = /\D/;
if ((allowedchars.test(document.cond.luftf.value)) || (allowedchars.test(parent.frames[1].navform.flow.value))) {
alert("ERROR");
err3 = 1;
...
}
-
Dec 4th, 2001, 04:59 AM
#2
Frenzied Member
Code:
<SCRIPT LANGUAGE="JAVASCRIPT"><!--
Function check(contents) {
If (((contents / contents) != 1) && (contents != 0)) {alert('Please enter only a number into this text box')}
}
//--></SCRIPT>
-
Dec 4th, 2001, 09:35 AM
#3
Frenzied Member
You can modify you matching string. If you want to allow numbers and -'s. Using parseInt() will give unpredictable results if you want to allow -'s. So will trying to divide the string into itself.
Let's say you wanted to match a US telephone number:
Code:
var allowed = /\(?\d{0,3}\)?-?\d{3}-\d{4}/;
You might have to escape the -'s, I'm not sure.
Travis, Kung Foo Journeyman
As always, RTFM.
WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
YBMS, but Mozilla doesn't.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|