0

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..

4
  • You only want to validate a textbox for having numeric values and should be a required field?? Commented Sep 26, 2012 at 9:47
  • 1
    /[^\d]/ matches all except digits it's a negated character class Commented Sep 26, 2012 at 9:47
  • @RatanSharma yes i want the same you explaining Commented Sep 26, 2012 at 9:51
  • @FabrizioCalderan ohhh ohk my mistake.. please help me what should i use there... Commented Sep 26, 2012 at 9:51

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

try the Texottela plugin, enables using a numeric() function for that, and you can supply a callback that runs when focus is lost and the value in the text box is not a valid number.

Comments

0

I believe the term you're looking for is a regular expression

This should do it:

var filter = /^\d*$/;

The carat ^ symbol will anchor the decimal to the start of the string The $ symbol anchors the decimal to the end of the string and the \d* matches one or more decimal values

If there is any non-decimal in the string, the regex will return false

Comments

0

You don't need a regex for that

    var a = Number($.trim(controlID));  //or parseInt($.trim(controlID))
    if (!isNaN(a)) {
        //a is valid number
    } else {
        //a is not a number or is empty
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.