rasellers0
rasellers0

Reputation: 95

using regular expressions to find non-numeric values in javascript/jquery code

Essentially, I am trying to find any non-numeric values a user enters into a text field. currently, my code looks like this:

    else if(MileInputElement.match([0-9]))
    {
        InvalidEntry= "Non-numeric values"; 
    }

However, when I run this, I get an error saying

Object doesn't support Property or Method "match."

So, what is a better way to handle this?

Upvotes: 0

Views: 4518

Answers (2)

hungndv
hungndv

Reputation: 2141

Here you are: \D for non-digit, g for all matches, match return an array

alert('a9bc8'.match(/\D/g));

Hope this helps.

Upvotes: 2

Avinash Raj
Avinash Raj

Reputation: 174706

Use regex delimiters and also you need to convert the object to string before applying the match function.

else if(JSON.stringify(MileInputElement).match(/[^0-9]/))
    {
        InvalidEntry= "Non-numeric values"; 
    }

Upvotes: 0

Related Questions