-1

I want to allow only @ . and _ as special characters in email but it always shows errors

html code in php file: //email

`echo "<td>email*</td><td><input type='text' id='regemail' name='regemail' onblur='javascript:valemail1()' onkeypress='javascript:hideemail()'></td>";`
`echo "</tr><tr id='emailerror'></tr>";`

javascript code(in function valemail1):

//regex
//if(!/^[a-zA-Z0-9]+$/.test(regemail)){
//document.getElementById('emailerror').innerHTML="<td><div id='redalert'>"+"only alphanumeric characters, an underscore (<b>_</b>), an (<b>@</b>) and a dot (<b>.</b>) are allowed in email"+"</div></td>";
//eemail=1;`
//return;`

//}else{
//}

 //no error
 if(eemail==0)
 {
document.getElementById('emailerror').innerHTML="";
 }

the function hideemail only hides error as

document.getElementById('emailerror').innerHTML="";

I actually dont know regex so please tell me what to type in it.

Sazzadur Rahaman
  • 6,938
  • 1
  • 30
  • 52
GTanmay
  • 19
  • 1
  • 3
  • Note that the requirement will disallow some valid email addresses, most importantly those with `-` or `+` in them. – JJJ Jun 18 '13 at 06:04
  • did you searched for email validation? There tons of questions about that on SO, for instance http://stackoverflow.com/questions/46155/validate-email-address-in-javascript?rq=1 – Wouter J Jun 18 '13 at 06:07
  • "I actually dont know regex" - then you should learn how to use it (https://www.google.com/search?q=regex+tutorial+php). – Nikola Jun 18 '13 at 06:07

4 Answers4

4

You can use this regex:

/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
stema
  • 90,351
  • 20
  • 107
  • 135
karthi
  • 889
  • 12
  • 24
0

Email Addresses are not as simple as you are anticipating in your question. But yes, you can expect that, most of the email addresses you will face, will be as simple as it can be. In that case a simple regex for validating email addresses is given bellow:

\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b

But if you want only to allow _ (along with @ and .) The expression will be (more simpler for you):

\b[A-Z0-9._]+@[A-Z0-9.]+\.[A-Z]{2,4}\b

And here You will find more regular expressions to validate your email address including the official standard: RFC 2822, compliant regexes, In case you want to validate almost all kinds of email addresses!

Community
  • 1
  • 1
Sazzadur Rahaman
  • 6,938
  • 1
  • 30
  • 52
  • No, that's not **the** well known regex for validating emails. This is: http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html (note, the regex for validating emails is 6.2kB long) – slebetman Jun 18 '13 at 06:17
  • RFC 2822 has been obsolete for years. – JJJ Jun 18 '13 at 06:30
0

I don't know if this is what you need but I'll just give it a try

var emailform = "^[-0-9A-Za-z!#$%&'*+/=?^_`{|}~.]+@[-0-9A-Za-z!#$%&'*+/=?^_`{|}~.]+";
if(!email.match(emailform)){
 document.getElementById('emailerror').innerHTML="Invalid email";
 return false;
}
Markipe
  • 616
  • 6
  • 16
0

this regex will meet your requirement:

^[\w._]+@(?:[\w-]+\.)+[\w]{2,6}$

for more information, please visit : Validate email address in JavaScript?

Community
  • 1
  • 1
Rayhan.iit.du
  • 279
  • 1
  • 4