-1

I'm creating an email field in my html, can I only accept one @?

For example email:

chong!#$@gmail.com - should invalid because of there are others special characters included

or

ch@ng@gmail.com - should also be invalid because there are two @'s.

The only accepted special character should only be one @, how do I do this in javascript/jquery? Sorry I really don't know much in regex. Or is there another way to validate an email format?

Addison
  • 3,791
  • 3
  • 28
  • 48
Chong We Tan
  • 233
  • 5
  • 15

3 Answers3

1

You can use the following regex in your input:

 <input type="email" pattern="[a-zA-Z0-9.]+\@[a-zA-Z0-9.]+\.[a-zA-Z]+" />
1

This pattern avoid the user input an 'email' that don't fits with the email standard but also avoid limited the number of characters input in the name of user to 64 characters and the number of characters in the domain too.

  • ^[A-Z0-9._%+-]{1,64}@(?:[A-Z0-9-]{1,63}.){1,125}[A-Z]{2,63}$

Some other patterns for validate numbers, numbers and letters and just letters:

  • ^[0-9]+$
  • ^[a-zA-Z0-9]+$
  • ^[a-zA-Z]+$

Also you can use regular expression with javascript like this Validate email address in JavaScript? and this other page its really useful for check if your regex pattern works correctly http://regexr.com/

Community
  • 1
  • 1
0

Try using this. It will open up a popup explaining the error if format is incorrect:

<form>
    <input pattern="[a-zA-Z0-9.]+\@[a-zA-Z0-9.]+\.[a-zA-Z]+" title="Write your error here" />
    <input type="submit" />
</form>

Hope this helps.

helfi
  • 125
  • 3
  • 10