Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

Help with Numeric Validation Variation

I use the following code to check for numeric value in fields. It does not check for symbols like $ , ' " etc... Can someone help me out with a reg expression that I can add to this simple method so the field can only contain numbers? One note. decimal point must be allowed. I know there are other methods like making an array of numbers and symbolds and looping through but I don't wish to use it if possible.

<script language=javascript>
function checkval() {
var x, r, ri;
x  = document.myform;
ri = /[A-Z]/i;

r  = x.myfield.value.search(ri);
if (r != -1) {
    alert ('this field must be numeric');
    return false;
    }
return true;
}
</script>
0
Bob-Villa
Asked:
Bob-Villa
  • 3
1 Solution
 
archrajanCommented:
<INPUT type="text" name="numeric" onkeyup='this.onchange();' onchange='this.value=this.value.replace(/[^\d]*/gi,"");' />
0
 
GwynforWebCommented:
<script language=javascript>
function checkval() {
r  = x.myfield.value
if (isNaN(r)) {
    alert ('this field must be numeric');
    return false;
    }
return true;
}
</script>

or

<script language=javascript>
function checkval() {
r  = x.myfield.value
if (/\D/.test(r)) {
    alert ('this field must be numeric');
    return false;
    }
return true;
}
</script>
0
 
Bob-VillaAuthor Commented:
if (isNaN(r)) worked like a charm.
0
 
GwynforWebCommented:
archrajan, try pasting non-numbers, blurring and then pasting again.
0
 
GwynforWebCommented:
this does work though

<INPUT type="text" name="numeric" onkeyup='this.onblur();' onblur='this.value=this.value.replace(/[^\d]*/gi,"");' >
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now