Skip to content
Advertisement

How would I make the html5 input to accept only email addresses from gmail.com using patter attribute?

I am using html input control to feed in email address. Also, I am using required attribute but I want the input should only be from gmail.com i.e. john@gmail.com, aamir@gmail.com etc and not any other domain.

<input type="text" name="email" pattern="^[a-zA-Z0-9]+@domain.[a-zA-z]+$" required>

But this accept every domain and not only gmail.com.

NOTE: I need to do it with regex in html5 control not with javascript etc

Update: I am using razor syntax with asp.net mvc.

Advertisement

Answer

You can use this regex

^[a-zA-Z0-9]+@gmail.com$

enter image description here

<form>
<input type="text" name="email" pattern="^[a-zA-Z0-9]+@gmail.com$" required></input>
<input type='submit' placeholder='submit'/>
</form>
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement