Pregunta

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;
}
¿Fue útil?

Solución 2

  1. for only numeric:

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

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

Otros consejos

You're only matching against a single character.

/^[^a-z]$/i

This ensures that the entire string is non-letters.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top