问题
I want to validate this form and I have the javascript written but when I press submit it doesnt do anything.
Only the pattern=[a-zA-Z]
work
function validation() {
var name = document.getElementById('name').value;
var surname = document.getElementById('surname').value;
var message = document.getElementById('message').value;
var email = document.getElementById('email').value;
if (name == '' || surname == '' || message == '' || email == '') {
document.getElementById("eresult").innerHTML = "All fields required"
return false;
} else if (name.length < 2) {
document.getElementById("eresult").innerHTML = "Name must be atleast 2 charachters"
} else {
return true;
}
}
<aside id="sidebar">
<div class="dark">
<form action="#" name="form" method="POST" onsubmit="return validation();">
<h3>Get A Quote</h3>
<div>
<label>Name</label><br>
<input type="text" name="name" id="name" placeholder="Name" pattern="[a-zA-Z]*">
</div>
<div>
<label>Surname</label><br>
<input type="text" name="surname" id="surname" placeholder="Surname" pattern="[a-zA-Z]*">
</div>
<div>
<label>Email</label><br>
<input type="email" name="email" id="email" placeholder="Email Address">
</div>
<div>
<label>Message</label><br>
<textarea type="message" name="message" id="email" minlength="1" maxlength="200" placeholder="Message(max 200 characters)"></textarea>
</div>
<input type="submit" name="submit" value="Submit">
</form>
<div id="eresult" style="color:red;"></div>
</div>
</aside>
<footer>
<p>Renars Web Design, Copyright © 2019</p>
</footer>
I expect if I dont enter all fields it should say "All Fields required" but it just submits the form.
回答1:
Using the "required" attribute:
Just add the required
attribute to each input if you want to make an input mandatory.
When this attribute is set, the form won't submit (and will display an error message) when the input is empty (the input will also be considered invalid).
Check the following Code Snippet and try to submit the form without filling some or all of the fields to see how the required
attribute works:
<form action="#" name="form" method="POST">
<div>
<label>Name</label><br>
<input type="text" name="name" id="name" placeholder="Name" pattern="[a-zA-Z]*" required>
</div>
<div>
<label>Surname</label><br>
<input type="text" name="surname" id="surname" placeholder="Surname" pattern="[a-zA-Z]*" required>
</div>
<div>
<label>Email</label><br>
<input type="email" name="email" id="email" placeholder="Email Address" required>
</div>
<div>
<label>Message</label><br>
<textarea type="message" name="message" id="email" minlength="1" maxlength="200" placeholder="Message(max 200 characters)" required></textarea>
</div>
<input type="submit" name="submit" value="Submit">
</form>
Using a JavaScript function:
There are a couple of things that you need to fix in your JS as well as HTML:
- Change your
input type="submit"
tobutton type="button"
so that the submit button will run the function first instead of directly trying to submit the form. Also, make sure to change thename
attribute value andid
attribute valuesubmit
to something else, likesubmitBtn
orsubmitForm
so that you can manually run thesubmit()
method later in the function. - Add an element with an id
"eresult"
where your validation function can push the error messages to. - Add a click listener to your submit button to run your validation function.
- Change the
id
attribute value of yourtextarea
element tomessage
. - Use the same
if/else statement
that you already have but add areturn false;
to yourelse if
statement too and add aform.submit()
to your finalelse
statement above thereturn true;
.
N.B. We had to change the name
and id
attribute of your submit button earlier so that form.submit()
will reference the submit()
method and not the submit button in the validation function.
Check and run the following Code Snippet for a practical example of what I had described above:
/* JavaScript */
document.querySelector("button").addEventListener("click", function(){
var name = document.getElementById('name').value;
var surname = document.getElementById('surname').value;
var message = document.getElementById('message').value;
var email = document.getElementById('email').value;
if(name=='' || surname=='' || message=='' || email==''){
document.getElementById("eresult").innerHTML = "All fields required"
return false;
}
else if(name.length<2){
document.getElementById("eresult").innerHTML = "Name must be atleast 2 charachters"
return false;
}
else{
form.submit();
return true;
}
});
<!-- HTML -->
<form action="#" name="form" method="POST">
<div>
<label>Name</label><br>
<input type="text" name="name" id="name" placeholder="Name" pattern="[a-zA-Z]*">
</div>
<div>
<label>Surname</label><br>
<input type="text" name="surname" id="surname" placeholder="Surname" pattern="[a-zA-Z]*">
</div>
<div>
<label>Email</label><br>
<input type="email" name="email" id="email" placeholder="Email Address">
</div>
<div>
<label>Message</label><br>
<textarea type="message" name="message" id="message" minlength="1" maxlength="200" placeholder="Message(max 200 characters)"></textarea>
</div>
<button type="button" name="submitBtn">Submit</button>
</form>
<div id="eresult"></div>
回答2:
You have an error in your code. View the console error log.
You have an incorrect id in the message textarea.
In other words, "email" identifier is duplicated.
来源:https://stackoverflow.com/questions/54216006/javascript-form-validation-function-is-not-working