Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/478.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript中电话号码字段的多个条件_Javascript_Jquery_Logic - Fatal编程技术网

javascript中电话号码字段的多个条件

javascript中电话号码字段的多个条件,javascript,jquery,logic,Javascript,Jquery,Logic,我有以下代码: $('.mobileNumber').keyup(function () { var mobile = $(this).val(); var numericExpression = /^[0-9]+$/; if (mobile.length == 0 || mobile.length < 10 || !mobile.match(numericExpression)) { $(this).parent().parent().parent().

我有以下代码:

$('.mobileNumber').keyup(function () {
   var mobile = $(this).val();
   var numericExpression = /^[0-9]+$/;

    if (mobile.length == 0 || mobile.length < 10 || !mobile.match(numericExpression)) {

     $(this).parent().parent().parent().find('.jp-error-msg-kstm').css('display','block');

 } else {

     $(this).parent().parent().parent().find('.jp-error-msg-kstm').css('display','none');
   }
});
$('.mobileNumber').keyup(函数(){
var mobile=$(this.val();
var numericExpression=/^[0-9]+$/;
if(mobile.length==0 | | mobile.length<10 | |!mobile.match(numericExpression)){
$(this).parent().parent().parent().find('.jp error msg kstm').css('display','block');
}否则{
$(this).parent().parent().parent().find('.jp error msg kstm').css('display','none');
}
});
我想做的是,只要我的mobileNumber字段为空(即o长度),长度小于10,并且应该只跟在数字后面。如果是,则会出现错误消息,即
.jp error msg kstm
,否则不会出现。
例如,如果输入1234567890,则不应显示错误,而如果删除该值并使字段长度为0或小于10,则应显示错误span div。

只需使用
.length

if (mobile.length < 10 || !mobile.match(numericExpression)) {
if(mobile.length<10 | |!mobile.match(numericExpression)){
我用过

mobile.length==0
仅在第一个if条件下

所以代码变成了

    $('.mobileNumber').keyup(function () {
        var mobile = $(this).val();
        var numericExpression = /^[0-9]+$/;

        if (mobile.length < 10 || !mobile.match(numericExpression)) {

          if(mobile.length == 0){
            $(this).parent().parent().parent().find('.jp-error-msg-kstm').css('display','none');
          } else {
            $(this).parent().parent().parent().find('.jp-error-msg-kstm').css('display','block');
          }
      } else {
        $(this).parent().parent().parent().find('.jp-error-msg-kstm').css('display','none');
       }
     });
$('.mobileNumber').keyup(函数(){
var mobile=$(this.val();
var numericExpression=/^[0-9]+$/;
if(mobile.length<10 | |!mobile.match(numericExpression)){
如果(mobile.length==0){
$(this).parent().parent().parent().find('.jp error msg kstm').css('display','none');
}否则{
$(this).parent().parent().parent().find('.jp error msg kstm').css('display','block');
}
}否则{
$(this).parent().parent().parent().find('.jp error msg kstm').css('display','none');
}
});

您遇到了什么问题?很抱歉,我使用了它,但忘了在这里提及。它仍然不起作用。