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>
LVL 1
Bob-VillaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.