[英]Form validation with clean Vue.js
I need to make clean vue.js based form validation, that checks input on-the-fly without page refresh.我需要进行干净的基于 vue.js 的表单验证,它可以在不刷新页面的情况下即时检查输入。
So far i`ve come to the following code for e-mail field validation到目前为止,我已经来到以下用于电子邮件字段验证的代码
<div class="container" id="forms" >
<label for="inputEmail" class="sr-only"></label>
<input type="email" id="inputEmail" class="form-control" placeholder="example@domen.com" required v-model='email'>
<div class="error" v-show="email && !isEmailValid"><span style="color:red;">Input Correct e-mail </span></div></div>
new Vue({
el: '#forms',
data: { email: ''},
computed: {isEmailValid: function isEmailValid() {
return (/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(this.email)
); } } });
But when i try to add other validation forms in same way/pattern, it breaks - it starts to display erroe message from the very beginning or doesnt display error at all no matter what have been inputed into the field.但是,当我尝试以相同的方式/模式添加其他验证表单时,它会中断 - 它从一开始就开始显示错误消息或根本不显示错误,无论输入到该字段中的内容如何。
Here is the link for the fiddle https://jsfiddle.net/CapablancaYEAH/vpk02stu/这是小提琴的链接https://jsfiddle.net/CapablancaYEAH/vpk02stu/
In scripts section i have that instance which im trying to make work, there is probably a mistake there, that breaks everything and i cant understand what should i fix exactly.在脚本部分,我有一个我正在尝试工作的实例,那里可能有一个错误,它破坏了一切,我无法理解我应该确切地修复什么。 Something is wrong with declaring or Syntax.
声明或语法有问题。 I`ve been trying different variants.
我一直在尝试不同的变体。
Can you please help me?你能帮我么?
You can only have one data and one computed properties.你只能有一个数据和一个计算属性。
the following is a working version of your code.以下是您的代码的工作版本。 I didn't do anything except add the fio v-model and combine your computed and data properties.
除了添加 fio v-model 并结合您的计算和数据属性之外,我什么也没做。 So , um, good job!
所以,嗯,干得好!
see: https://jsfiddle.net/vpk02stu/5/见: https : //jsfiddle.net/vpk02stu/5/
code to satisfy stackoverflow standards:满足stackoverflow标准的代码:
new Vue({
el: '#forms',
data: { email: '', fio: ''},
computed: {
isEmailValid: function isEmailValid() {
return (/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(this.email)
);
},
isFioValid: function isFioValid() {
return (/\w+[ ]+\w+[ ]+\w+/.test(this.fio)
);
}
}
});
<div class="container" id="forms" >
<h3 style="text-align:center;">Fill the fields</h3>
<form class="form-signin">
<label for="inputFIO" class="sr-only" >ФИО</label>
<input type="text" id="inputFIO" class="form-control" placeholder="Surname and Name" required v-model="fio">
<div class="error" v-show="fio && !isFioValid"><span style="color:red;">Input name and surname</span></div>
<label for="inputEmail" class="sr-only"></label>
<input type="email" id="inputEmail" class="form-control" placeholder="example@domen.com" required v-model='email'>
<div class="error" v-show="email && !isEmailValid"><span style="color:red;">Введите корректный e-mail </span></div>
<label for="inputText"></label>
<textarea class="form-control" id="inputText" rows="4"></textarea>
<button class="btn btn-lg btn-primary btn-block" type="submit">Submit</button>
</form>
</div>
body {
padding-top: 40px;
padding-bottom: 40px;
background-color: #eee;
}
.form-signin {
max-width: 430px;
padding: 15px;
margin: 0 auto;
}
.form-signin .form-signin-heading,
.form-signin {
margin-bottom: 10px;
}
.form-signin .checkbox {
font-weight: normal;
}
.form-signin .form-control {
position: relative;
height: auto;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
padding: 10px;
font-size: 16px;
}
.form-signin .form-control:focus {
z-index: 2;
}
.form-signin input[type="email"] {
margin-bottom: 10px;
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
}
.form-signin input[type="text"] {
margin-bottom: 10px;
border-top-left-radius: 0;
border-top-right-radius: 0;
}
.btn {
max-width: 200px;
margin: 0 auto;
margin-top: 15px;
}
In Angular, we have a built-in option to validate forms.在 Angular 中,我们有一个内置选项来验证表单。 But Vue offers very limited functionality when it comes to create and validate forms.
但是 Vue 在创建和验证表单时提供的功能非常有限。 To add more functionality, we have to install a separate package called Vuelidate to validate our Vue application forms.
为了添加更多功能,我们必须安装一个名为 Vuelidate 的单独包来验证我们的 Vue 应用程序表单。
What is Vuelidate?什么是维利达?
According to the Vuelidate website :根据 Vuelidate网站:
“Vuelidate 2 is a simple, but powerful, lightweight model-based validation for Vue.js 3 and Vue 2.x.” “Vuelidate 2 是一个简单但功能强大的轻量级基于模型的验证,适用于 Vue.js 3 和 Vue 2.x。”
It allows us to use built-in as well as custom validators它允许我们使用内置和自定义验证器
Install安装
npm install @vuelidate/core @vuelidate/validators
Reference:参考:
https://aaqibqs.medium.com/learn-form-validation-in-vue-3-in-10-minutes-with-vuelidate-8929c5059e66 https://aaqibqs.medium.com/learn-form-validation-in-vue-3-in-10-minutes-with-vuelidate-8929c5059e66
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.