Reputation: 663
im trying to validate an email address with this regular expression .*@.*\..*
im just wondering how i can change this to also check if the string is more than one character?
Upvotes: 0
Views: 641
Reputation: 5727
Use below for email validation
function chkEmail(theField,msg)
{
var a=theField.value;
var reg_mail=(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+(\.[A-Za-z0-9]{2,3})(\.[A-Za-z0-9]{2,3})?$/);
if((a.search(reg_mail)==-1))
{
alert(msg);
theField.focus();
return false;
}
return true;
}
Use below for length
if(document.getElementById('id').value.length > 1)
{
}
Upvotes: 0
Reputation: 700152
Use the +
specifier instead of *
to make sure that there is at least one character:
.+@.+\..+
This will actually ensure that there is at least five characters, as you can't have a public email address that makes sense with less than that. You could make a more elaborate check (for example based on the minimum length of domain names and the allowed characters), but this at least covers the most basic requirements.
Upvotes: 3
Reputation: 7475
Validating email addresses with a regex is non-trivial. See here and here.
Upvotes: 0
Reputation: 61729
You don't need to do length checks with Regex, just use the string length option:
string Email = "[email protected]";
// Regex checks here
if(Email.Length > 1){
}
Also I would recommend not validating email addresses. It's insanely complicated, see this question for more information:
Using a regular expression to validate an email address
Upvotes: 1