0
<script type = "text/javascript">
Is there anything wrong with my validateEmail function that is causing it not to work properly? https://pastebin.com/KQZSHMn9
4
  • We need to see your function to determine what's going on. Also, more details about "not to work properly". What's a test case that fails? How is it failing? Commented Apr 22, 2022 at 18:52
  • pastebin.com/KQZSHMn9 Here it is. It puts up an error message when I don't enter anything in but it doesn't fail when I don't put in "@" or anything else like that. Commented Apr 22, 2022 at 18:55
  • Please include your code in your post, not as a link or an image. Why is uploading images of code on Stack Overflow such a big "no no"? Commented Apr 22, 2022 at 18:56
  • It won't let me include it as code for some reason. I copy and pasted it where it told me to but it wouldn't work. I had to put it in pastebin instead. Commented Apr 22, 2022 at 18:58

1 Answer 1

2

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>

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you. But i'm not sure where I went wrong in my code. Would you be able to explain to me please?
imgur.com/a/QtqpgwC I'm not sure what's wrong with my validateEmail function.
pastebin.com/KQZSHMn9 here it is in pastebin.
@MattyB323 Use regex.
does it not work in pastebin?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.