2017-01-20 47 views
-1

我在我的HTML这样的两个输入字段:正则表达式只允许正整数,并检查有效性的jQuery

<input type="text" class="txtminFeedback" pattern="^\d+([\.\,][0]{2})?$" placeholder="Minimum Feedback"> 

<input type="text" class="txtmaxFeedback" pattern="^\d+([\.\,][0]{2})?$" placeholder="Maximum Feedback"> 

我已经试过像以下几个正则表达式模式:

^\d+([\.\,][0]{2})?$ 

or 

(^[0-9]+$|^$) 

or 

/^\d*$/ 

以下代码在jQuery中都不起作用:

if ($('.txtminFeedback').val() == "" && $('.txtmaxFeedback').val() == "") { 
    if ($('.txtmin')[0].checkValidity() && $('.txtmax')[0].checkValidity()) { 
     if ($('.txtSearch').val() == "") { 
      ShowMessage("Please enter the search term!"); 
      return; 
     } 
     else { 
      PostAndUpdate($('.txtSearch').val(), $('input[name=type]:checked').val(), $('input[name=shipping]:checked').val(), $('input[name=condition]:checked').val(), $('.txtmin').val(), $('.txtmax').val(), $('.txtNegativeKeywords').val(), $('.txtminFeedback').val(), $('.txtmaxFeedback').val()); 
     } 
    } else { 
     ShowMessage("You have entered incorrect value for minimum or maximum price!"); 
     return; 
    } 
} else if (!$('.txtminFeedback')[0].checkValidity() || !$('.txtmaxFeedback')[0].checkValidity()) 
{ 
    ShowMessage("Please enter only positive value for minimum and maximum feedback."); 
    return; 
} 

用户可以离开txtminfeedback a nd txtmaxfeedback如果他愿意,则为空。但是,如果他决定输入一些数值,则必须输入两个字段,并且只需要输入完整的正数(从0到4百万)。

我在这里做错了什么?

+1

''? –

+0

@AlexK。我忘了提及即使我用这3种正则表达式在字段中输入完整的正数,也没有任何反应。例如,当我进入1到5000,什么也没有发生,但它应该... –

+1

也许你需要清理的正则表达式位:'模式= “\ d +(0 {2} [。])?”'? –

回答

0

这到底做到了:

pattern="^(\s*|\d+)$" 


if ($('.txtminFeedback')[0].checkValidity()==false || $('.txtmaxFeedback')[0].checkValidity()==false) { 
       ShowMessage("Please enter only positive value for minimum and maximum feedback."); 
       return; 
      } 
      if ($('.txtmin')[0].checkValidity() && $('.txtmax')[0].checkValidity()) { 
       if ($('.txtSearch').val() == "") { 
        ShowMessage("Please enter the search term!"); 
        return; 
       } 
       else { 
        PostAndUpdate($('.txtSearch').val(), $('input[name=type]:checked').val(), $('input[name=shipping]:checked').val(), $('input[name=condition]:checked').val(), $('.txtmin').val(), $('.txtmax').val(), $('.txtNegativeKeywords').val(), $('.txtminFeedback').val(), $('.txtmaxFeedback').val()); 
       } 
      } else { 
       ShowMessage("You have entered incorrect value for minimum or maximum price!"); 
       return; 
      } 

万一有人将来会需要它。

Cheers =)

相关问题