Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate that text field is numeric usiung jQuery

Tags:

I have a simple issue -- I would like to check a field to see if it's an integer if it is not blank. I'm not using any additional plugins, just jQuery. My code is as follows:

if($('#Field').val() != "") {     if($('#Field').val().match('^(0|[1-9][0-9]*)$'))     {         errors+= "Field must be numeric.<br/>";         success = false;     } } 

...It doesn't seem to work. Where am I going wrong?

The error I receive is val() is not an object.


It turned out that the real issue was that I had my element name set and not the Id.

like image 380
George Johnston Avatar asked Mar 09 '10 00:03

George Johnston


1 Answers

Regex isn't needed, nor is plugins

if (isNaN($('#Field').val() / 1) == false) {     your code here } 
like image 184
motomod Avatar answered Sep 20 '22 12:09

motomod