-1

I am wrting a javascript if the accepted value is other than alphanumeric characters and symbols _.-@ , it should throw an alert.

I have used below expression, however concern is that I need only one mandate occurence of @ symbol. How can I do that?

!/^[ A-Za-z0-9_.-@]*$/
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
Sneha
  • 83
  • 1
  • 11
  • 1
    Can you give some sample inputs and whether they should be matched? – user229044 Jul 03 '18 at 20:06
  • you could check if the value contains the @ `str.contains('@')` if it is okay to not be 100% regex – Get Off My Lawn Jul 03 '18 at 20:07
  • Try `/^[ A-Za-z0-9_.-]*@[ A-Za-z0-9_.@-]*$/`. If the `@` can only appear once, use `/^[ A-Za-z0-9_.-]*@[ A-Za-z0-9_.-]*$/`. It would be best if you could supply some strings that should be (in)?valid. – Wiktor Stribiżew Jul 03 '18 at 20:07
  • 2
    Are you trying to write a regex to validate an email address? There are plenty of these online if you google for them (and if you are not trying to do an email address, still google for an email regex and see how it works - they will all only allow 1 `@` character) – Nathan Russell Jul 03 '18 at 20:07
  • matching strings can be - risha123@ however strings like risha@@ or risha should not be acceptable. So, @ is mandatory and should occur once only. @meagar – Sneha Jul 03 '18 at 20:26
  • @Risha What about `a@b`, `ab@` and `@ab`? Are these all valid? – user229044 Jul 03 '18 at 20:44
  • 1
    @Risha I see from your comments below that you *are* trying to validate email addresses. **Don't do this**. Email cannot be effectively validated with a regular expression. You need to validate the email by delivering mail to it, and confirming that the user received the email by opening an activation link, *not* by using a regular expression. Everything you've described so far is *wrong* and will fail for many valid email addresses. – user229044 Jul 03 '18 at 20:46
  • Regular expressions work if you only care about format, and not if it is an existing email or not. – Get Off My Lawn Jul 03 '18 at 20:49
  • 1
    @GetOffMyLawn No, they really don't. You can *at best* check that **at least one** `@` appears, anything else is likely to be wrong. Email address validation needs to be *extremely* permissive, and rely on delivering mail to the address. – user229044 Jul 03 '18 at 20:50

2 Answers2

4

You could look for as many accepted characters that aren't '@', then look for exactly one '@', followed by any amount of accepted characters again. The \w metacharacter will also replace your A-Za-z0-9_ portion, so you can use the following:

!/^[ \w.-]*@[ \w.-]*$/
Jacob Boertjes
  • 963
  • 5
  • 20
-1

If it is okay to not be completely Regex driven, you can use includes to see if the @ is already in the string. If it is we don't allow it otherwise we do allow it.

If the character isn't a @ we then test it against the regex.

document.getElementById('input-test').addEventListener('keydown', e => {
  let value = e.currentTarget.value
  let key = e.key
  if ((value.includes('@') && key == '@') || !!key.match(/[\sA-Za-z0-9_.\-@]/) === false) e.preventDefault()
})
<input type="text" id="input-test">
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
  • I have used a simple Javascript - Risha_EmailValidation () { var Name = serviceForm.Risha_Dict_1.Emailaddress.getValue(); if (!/^[ A-Za-z0-9_.-@]*$/.test(Name)) { alert("Email address can contain alphanumeric characters and the symbols- Hyphen (-), underscore (_) or period (.). It must contain one @ symbol."); } } – Sneha Jul 03 '18 at 20:34
  • @Risha That's not true at all for email address. They can contain `+`, as well as *tends of thousands* of other characters. Bad email validations are horrible for your users, please rethink your approach. – user229044 Jul 03 '18 at 20:47
  • it is not for end user experience, it is for research purpose and i have to do it through regex only. @meagar – Sneha Jul 03 '18 at 20:49