skip to Main Content

I have a input for phone number, and its type is not number is text, and I want to check if the input has characters to validate it as wrong, I have set it like that because I put a format to the input like 123-123-1234
here is my input

<input (keyup)="format()" (change)="format()" maxlength="12" inputmode="numeric" type='text' class="input" formControlName="celular" id="celular" name="celular">

Here is my ts where I set the format

  format(){
    $('#celular').val($('#celular').val().replace(/^(d{3})(d{3})(d+)$/, "$1-$2-$3"));
  }

so what I want to do, is to know if the value of my input has characters from aA-zZ and some specials characters that are not –

3

Answers


  1. Chosen as BEST ANSWER

    I have found the solution

    var b = /[a-z A-Z]/
    var a = '123a'
    b.test(a)
    

    this gives true or false depending if the var a has any character from a-z and A-Z


  2. With a little help from google i found this:

    Regex phoneRegex =
        new Regex(@"^(?([0-9]{3}))?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$");
    
    if (phoneRegex.IsMatch(subjectString)) {
        string formattedPhoneNumber =
            phoneRegex.Replace(subjectString, "($1) $2-$3");
    } else {
        // Invalid phone number
    }
    
    Login or Signup to reply.
  3. You can use html regex pattern for validating input fields

    Example:

    Format used in this example is 123-123-1234

    <form>
    <input type="text" placeholder="Enter Phone Number in format (123-123-1234)" pattern="^d{3}[-]d{3}[-]d{4}$" title="Invalid input" size="50" />
    <button type="submit">Submit</button>
    </form>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search