ceki10
ceki10

Reputation: 81

Pattern for email or telephone number

I have problem with regular expression in pattern for telephon number OR email

Code:

<input class="form__input" type="text" name="phone-or-email" placeholder="Telephone or email" pattern="^[0-9]{9}$ || [A-Za-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}" title="Telephone or email" required="required">

Pattern:

pattern="^[0-9]{9} || [A-Za-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$

telephone regular expression: ^[0-9]{9,12}$ - it's enough for me

email regular expression:

[A-Za-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}

it's ok email regular expression for me

Above code doesn't work :(

I would like have one input and one pattern for telephone number and email address

Upvotes: 3

Views: 4615

Answers (2)

Firoz Ansari
Firoz Ansari

Reputation: 67

<input  class="form-control" placeholder="Enter sender mobile no..." type="text"   pattern="[6-9]{1}[0-9]{9}|[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$" title="Please enter valid mobile number" required>

In my case above pattern is validating email and phone (starting from 6, 7, 8 or 9) as per Indian standard mobile number

Upvotes: 1

user9849588
user9849588

Reputation: 607

Try with:

pattern="^([0-9]{9})|([A-Za-z0-9._%\+\-]+@[a-z0-9.\-]+\.[a-z]{2,3})$"

With regular expressions you use a single "|" to represent OR. I also put telephone pattern and email pattern between parenthesis, so it would accept the telephone pattern or the email.

Upvotes: 6

Related Questions