如何验证长度和匹配所需的字符 [英] How to validate length and match required character
问题描述
这是我如何验证我所需的字符和长度
JavaScript
This how i validate my required characters and the length
JavaScript
function submitChangej(){
var firstname = /^[a-zA-Z-\s]{2,128}$/;
var lastname = /^[a-zA-Z-\s]{2,128}$/;
var inputlastName = document.getElementById("lastname");
var inputfirstName = document.getElementById("firstname");
var inputSubmit = document.getElementById("apply");
var Container = document.getElementById('Agreement');
//if((firstname.value.match(firstname)) || (lastname.value.match(lastname))
if((inputfirstName.value.length < 2 || inputfirstName.value.length > 128 ) || ( inputlastName.value.length < 2 || inputlastName > 128 )){
Container.style.display = 'none';
}
else{
Container.style.display = 'block';
}
}
</script>
如果值与所需类型不匹配,我想隐藏按钮,我可以隐藏它的长度,但我的匹配不起作用。 />
我在我验证的字段上调用我的方法
I want to hide the button if value does not match the required type, I m able to hide it on the length, but my match does not work.
I than call my method
submitChangej()
。
我已经评论了其他代码,但是没有匹配的代码。
您将不胜感激
on the fields that i validate.
I have commented other code, that didn't work the match one.
Your will be appreciated
推荐答案
/;
var lastname = / ^ [a-zA-Z-\s] { 2 , 128 }
/; var lastname = /^[a-zA-Z-\s]{2,128}
/;
var inputlastName = document .getElementById( lastname);
var inputfirstName = document .getElementById( firstname);
var inputSubmit = document .getElementById( apply);
var Container = document .getElementById(' 协议);
// if((firstname.value.match(firstname))||(lastname.value.match (姓氏))
if ((inputfirstName.value.length< 2 || inputfirstName.value.length> 128 )||(inputlastName.value.length< 2 || inputlastName> 128 )){
Container.style.display = ' none';
}
else {
Container.style.display = ' block';
}
}
< / script>
/; var inputlastName = document.getElementById("lastname"); var inputfirstName = document.getElementById("firstname"); var inputSubmit = document.getElementById("apply"); var Container = document.getElementById('Agreement'); //if((firstname.value.match(firstname)) || (lastname.value.match(lastname)) if((inputfirstName.value.length < 2 || inputfirstName.value.length > 128 ) || ( inputlastName.value.length < 2 || inputlastName > 128 )){ Container.style.display = 'none'; } else{ Container.style.display = 'block'; } } </script>
如果值与所需类型不匹配,我想隐藏按钮,我可以隐藏它的长度,但我的匹配不起作用。 />
我在我验证的字段上调用我的方法
I want to hide the button if value does not match the required type, I m able to hide it on the length, but my match does not work.
I than call my method
submitChangej()
。
我已经评论了其他代码,但是没有匹配的代码。
您将不胜感激
on the fields that i validate.
I have commented other code, that didn't work the match one.
Your will be appreciated
我认为您的问题是您使用的是正则表达式变量和控件的相同名称。
您是否尝试过这两组都有不同的命名方案吗?
I think your problem is that you are using the same name for your regular expression variables, and for your controls.
Have you tried to give both these groups different naming schemes?
这篇关于如何验证长度和匹配所需的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!