按类型和大小对变量进行JavaScript验证
由于javascript向该验证和显示警报框表示wnt,因此无论该字符串是否仅包含数字,并且大小必须固定为6长度。还显示是否要检查字符串是否只包含字母?按类型和大小对变量进行JavaScript验证,javascript,php,jquery,ajax,twitter-bootstrap,Javascript,Php,Jquery,Ajax,Twitter Bootstrap,由于javascript向该验证和显示警报框表示wnt,因此无论该字符串是否仅包含数字,并且大小必须固定为6长度。还显示是否要检查字符串是否只包含字母? 谢谢。您可以使用正则表达式检查您的要求: $(document).ready(function(){ $("#regisBtn").click(function(){ var pincode=$("#inputpcode3").val(); if(pincode){ //vali
谢谢。您可以使用正则表达式检查您的要求:
$(document).ready(function(){
$("#regisBtn").click(function(){
var pincode=$("#inputpcode3").val();
if(pincode){
//validation for pincode to check whether the pincode is only numeric or not and the size must be 6 only
}
else
{
alert("wrong");
}
});
});
如上所述,正则表达式可能是您最好的选择
if(/^\d{6}$/.test(pincode)){
// Passed
}
这将检查字符串,并仅在存在数字时进行验证
另外,检查长度只需if($(this).val().length>6){doSomethingElse()| | disable Submit Button}
请查看这个很棒的在线资源:
(确实有助于澄清)
if(/^\d{6}$/.test(pincode)){
// Passed
}
/^[0-9]*$/