Home > Software design >  Check if string that must be just numbers has characters
Check if string that must be just numbers has characters

Time:12-01

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

CodePudding user response:

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
}

CodePudding user response:

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>

  • Related