function submitForm() {
document.getElementById("form1").submit();
}
function validate() {
var ValidName = /^[a-zA-Z ]{2,30}$/;
var phoneno = /^d{10}$/;
var mailformat = /^[a-zA-Z0-9.!#$%&'+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:.[a-zA-Z0-9-]+)$/;
var ValidAddress = /^[a-zA-Z0-9s,'-]+$/;
var cityRegex = /^[a-zA-z] ?([a-zA-z]|[a-zA-z] )*[a-zA-z]$/
var validPostcode = /^[0-9]+$/;
var ValidMessage = /^[a-zA-Z0-9s,'-]+$/;
var a = document.forms["form1"]["message"].value;
if (!document.getElementById("username").value.match(ValidName)) {
alert("Please Enter A Valid First Name");
return false;
}
if (!document.getElementById("password").value.match(ValidName)) {
alert("Please Enter A Valid Last Name");
return false;
}
alert {"success"};
}
function success() {
alert("success")
}
<form class="login-form" action="#" name="form1" id="form1">
<input type="text" id="username" name="username" placeholder="username">
<input type="text" id="password" name="password" placeholder="password">
<button onclick="validate()">Login</button>
<p class="message">Not registered? <a href="Register.html">Create an account</a> </p>
</form>
the validate function is not working, there is no pop up although I leave the field empty and submit, it just bring me back to the top of the page.
But there is pop up if i change the onclick="validate()" to onclick="success()", so i guess its not the button’s problem. Then what problem is it?
2
Answers
There is a little syntax error in your code
should be
Your if logic is not correct. Also, you cannot use {} bracket with alert. It must be (). Below code works perfectly fine for me (I consider ValidName variable for explanation).