MattyB323
MattyB323

Reputation: 7

Can't validate e-mail with javascript function

<script type = "text/javascript">
Is there anything wrong with my validateEmail function that is causing it not to work properly? https://pastebin.com/KQZSHMn9

Upvotes: 0

Views: 63

Answers (1)

Chen890
Chen890

Reputation: 31

Your code isn't complete; I guess this is because you are a new contributor. There is an example for email validator code:

function ValidateEmail(inputText)
{
var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if(inputText.value.match(mailformat))
{
alert("Valid email address!");
document.form1.text1.focus();
return true;
}
else
{
alert("You have entered an invalid email address!");
document.form1.text1.focus();
return false;
}
}
li {list-style-type: none;
font-size: 16pt;
}
.mail {
margin: auto;
padding-top: 10px;
padding-bottom: 10px;
width: 400px;
background : 
#D8F1F8;
border: 1px soild 
silver;
}
.mail h2 {
margin-left: 38px;
}
input {
font-size: 20pt;
}
input:focus, textarea:focus{
background-color: 
lightyellow;
}
input submit {
font-size: 12pt;
}
.rq {
color: 
#FF0000;
font-size: 10pt;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript form validation - checking email</title>
<link rel='stylesheet' href='form-style.css' type='text/css' />      
</head>
<body onload='document.form1.text1.focus()'>
<div class="mail">
<h2>Input an email and Submit</h2>
<form name="form1" action="#"> 
<ul>
<li><input type='text' name='text1'/></li>
<li>&nbsp;</li>
<li class="submit"><input type="submit" name="submit" value="Submit" onclick="ValidateEmail(document.form1.text1)"/></li>
<li>&nbsp;</li>
</ul>
</form>
</div>
<script src="email-validation.js"></script>
</body>
</html>

Upvotes: 2

Related Questions