-3

I want a regular expression which should accept only digits, alphabets,no special character at the starting character and after that it can accept all the characters but it should not accept more than one dot whether the dot is continous or anywhere which means-

chethan.salimath@gmail.com - it should accept has valid
(chethan..salimath@gmail.com) - has invalid
chethan.sali.math@gmail.com - invalid 
Chethan Gs
  • 37
  • 8
  • 3
    Does this answer your question? [How to validate an email address in JavaScript](https://stackoverflow.com/questions/46155/how-to-validate-an-email-address-in-javascript) – thanhdx Feb 07 '20 at 06:38

2 Answers2

0
  • Get the value from input
  • split it on '@'
  • count . using filter
  • If more than 2 then giver error

function func(s) {
  s.split('').filter(e => e == '.').length >= 2 ? alert("Email cannot have 2 '.'") : alert("Nice")
}
<input/>
<button id="a" onclick="func(document.querySelector('input').value.split('@')[0])">click</button>
ellipsis
  • 12,049
  • 2
  • 17
  • 33
0

You can create a substring starting with characters from 0 till the first @. For example this a.substring(0, a.indexOf("@")) will create a substring like chethan.salimath or chethan..salimath. Now use String.match(/[.]/g) to get a array with all the dots(.). If the length of the array is more than 1 then the substring is invalid.

function matchDot(a) {
  1 === a.substring(0, a.indexOf("@")).match(/[.]/g).length ? 
        console.log("valid") : 
        console.log("invalid");
};
matchDot('chethan.salimath@gmail.com')
matchDot('chethan..salimath@gmail.com')
matchDot('chethan.sali.math@gmail.com')
brk
  • 48,835
  • 10
  • 56
  • 78