Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alert if variable contains non numeric characters?

'p' can only store "$", commas, dots or numbers.

How can I show an alert if it contains any other character?

like image 470
lisovaccaro Avatar asked Apr 24 '12 05:04

lisovaccaro


2 Answers

if (p.match(/[^$,.\d]/))
    alert('error!');

Live DEMO

You can use this Excellent regex cheat sheet.

like image 82
gdoron is supporting Monica Avatar answered Oct 12 '22 23:10

gdoron is supporting Monica


Consider:

if (/[^$,\.\d]/.test(p)) {
  // value has characters other than $ , . 0-9.
};

The regular expression test method returns a boolean value, whereas match returns an array and so is dependent on type conversion when used in a simlar manner.

like image 35
RobG Avatar answered Oct 13 '22 00:10

RobG