本文介绍了我想在检查验证后删除先前输入字段的错误消息,然后转到下一个输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
function validation(){
var name = document.getElementById('name').value;
var email = document.getElementById('email').value;
var password = document.getElementById('password').value;
var password2 = document.getElementById('password2').value;
var mobile = document.getElementById('mobile').value;
var address = document.getElementById('address').value;
var zip = document.getElementById('zip').value;
var country = document.getElementById('country').value;
if(name == ""){
document.getElementById("name_error").innerHTML = " ** Please fill the username field";
return false;
}
if((name.length <=2) ||(name.length>20)){
document.getElementById("name_error").innerHTML = " ** Username must be between 2 to 20 characters";
return false;
}
if (!isNaN(name)) {
document.getElementById("name_error").innerHTML = " ** Only characters are allowed";
return false;
}
if (email == "") {
document.getElementById("email_error").innerHTML = " ** Please fill email id field";
return false;
}
if (email.indexOf('@') <= 0) {
document.getElementById("email_error").innerHTML = " ** Email id not valid, @ position is wrong";
return false;
}
if ((email.charAt(email.length-4)!='.') && (email.charAt(email.length-3)!='.')) {
document.getElementById("email_error").innerHTML = " ** Email id not valid, . position is wrong";
return false;
}
if(password == ""){
document.getElementById("password_error").innerHTML = " ** Please fill the password field";
return false;
}
if(password.length < 8){
document.getElementById("password_error").innerHTML = " ** Password must be of atleast 8 characters";
return false;
}
if(password != password2){
document.getElementById("password2_error").innerHTML = " ** Password are not matching";
return false;
}
if(password2 == ""){
document.getElementById("password2_error").innerHTML = " ** Please fill the confirm password field";
return false;
}
if(mobile == ""){
document.getElementById("mobile_error").innerHTML = " ** Please fill the mobile field";
return false;
}
if (isNaN(mobile)) {
document.getElementById("mobile_error").innerHTML = " ** Use digits in mobile number";
return false;
}
if(mobile.length != 10){
document.getElementById("mobile_error").innerHTML = " ** Use valid mobile number";
return false;
}
if(address == ""){
document.getElementById("address_error").innerHTML = " ** Please fill the address field";
return false;
}
if(zip == ""){
document.getElementById("zip_error").innerHTML = " ** Please fill the zip code field";
return false;
}
if (isNaN(zip)) {
document.getElementById("zip_error").innerHTML = " ** Use digits in zip code";
return false;
}
if(country == "Default"){
document.getElementById("country_error").innerHTML = " ** Select your country";
return false;
}
return true;
}
我的尝试:
What I have tried:
<!DOCTYPE html>
<title>Form Validation
<div class="container">
<h1 class="text-success text-center">Form Validation</h1>
<div class="col-md-6 m-auto d-block">
<div class="form-group">
Username:
<div id="name_error" class="text-danger"></div>
</div>
<div class="form-group">
Email:
<div id="email_error" class="text-danger"></div>
</div>
<div class="form-group">
Password:
<div id="password_error" class="text-danger"></div>
</div>
<div class="form-group">
Confirm Password:
<div id="password2_error" class="text-danger"></div>
</div>
<div class="form-group">
Mobile:
<div id="mobile_error" class="text-danger"></div>
</div>
<div class="form-group">
Address
<div id="address_error" class="text-danger"></div>
</div>
<div class="form-group">
ZIP Code
<div id="zip_error" class="text-danger"></div>
</div>
<div class="form-group">
Country
(Please select a country)
India
Pakistan
USA
China
Japan
<div id="country_error" class="text-danger"></div>
</div>
</div>
</div>
推荐答案
document.getElementById("name_error").innerHTML = "";
等。
etc.
这篇关于我想在检查验证后删除先前输入字段的错误消息,然后转到下一个输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!