I want to validate a textbox have numeric values using the following code, but i dont know the filter term there for numeric, my code is:
function validateRequiredNumeric(Control, msgInfo) {
var ControlId = $('#' + Control);
var msgInfoId = $('#' + msgInfo);
//testing regular expression
var a = ControlId.val();
var filter = /[^\d]/;
alert(a.length);
if (a.length != 0) {
if (filter.test(a)) {
msgInfoId.text('');
ControlId.css({ 'border': '1px solid green' });
return true;
} else {
//msgInfoId.css({ 'color': 'red', 'font-size': '12px', 'font-style': 'italic' });
msgInfoId.text("Numeric Onlydd fdf dfd");
ControlId.css({ 'border': '1px solid red' });
return false;
}
}
else {
//msgInfoId.css({ 'color': 'red', 'font-size': '12px', 'font-style': 'italic' });
msgInfoId.text("You can't leave this empty.");
ControlId.css({ 'border': '1px solid red' });
return false;
}
}
Please help me.. i have tried the “[^0-9]” this also..
The regexp should be either:
For only numeric
/^[\d]+$/For any non-numeric
/^[^\d]+$/I am not sure what you mean in your question since your regexp try was for non-numeric.
Another answer made me think about it, if you expect users to use decimals, you should add some character cases…
For decimals
/^[\d]+[,.]?[\d]*$/requires at least one number before the decimal.