简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM