Question

i'm trying to validate a field to include everything except letters but the following only works on the first character i enter. So if i enter '123a' the test method returns true.

$.tools.validator.fn("input#Phone", "Please enter a valid phone number.", function(input, value) {  
    var pass;
    var rgx = /[^a-z]/gi;

    if ( rgx.test(value) 
         || (value == "") 
         || (value == $(input).attr("placeholder"))) {  

        $(input).removeClass("invalid");            
        pass = true;            
    } else {                    
        $(input).addClass("invalid");           
        pass = false;       
    }

    return pass;
}
Was it helpful?

Solution 2

  1. for only numeric:

      RegExp(/^[^a-zA-Z]$/i)
    
  2. for phone number you can use

     RegExp(/^[0-9 -()+]{6,20}$/i)
    

OTHER TIPS

You're only matching against a single character.

/^[^a-z]$/i

This ensures that the entire string is non-letters.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top