下面的JavaScript只检查字段中是否有4位数字,然后警告是否存在。
但由于某种原因,如果我输入四个零(0000),则将其作为一个空字段并抛出警告。
任何想法如何解决这个问题???我不是程序员,但是在经过数周的反复试验之后将这些代码放在一起。
function validate(){
// if ( (!isNaN($("#couponstart1").val())) || (!isNaN($("#couponstart2").val())) || (!isNaN($("#couponend1").val())) || (!isNaN($("#couponend2").val())) ) {
var all_ok = true;
var err_msg = "";
var fld = "";
if ( $("#couponstart1").val() == '' || $("#couponstart2").val() == '' || $("#couponend1").val() == '' || $("#couponend2").val() == '' ) {
all_ok = false;
err_msg += "\n - Card Numbers cannot be blank";
fld='couponstart1';
}else{
if ( isNaN($("#couponstart1").val()) || isNaN($("#couponstart2").val()) || isNaN($("#couponend1").val()) || isNaN($("#couponend2").val()) ) {
all_ok = false;
err_msg += "\n - Card Number has to be numeric";
fld='couponstart1';
}else{
if ( $("#couponstart1").val() < 1 || $("#couponstart2").val() < 1 || $("#couponend1").val() < 1 || $("#couponend2").val() < 1 ) {
all_ok = false;
err_msg += "\n - Card Numbers are not correct";
fld='couponstart1';
}else if ($("#couponstart1").val().length != 4 || $("#couponstart2").val().length != 4 || $("#couponend1").val().length != 4 || $("#couponend2").val().length < 4){
all_ok = false;
err_msg += "\n - Card Numbers are not correct";
fld='couponstart1';
}
}
}
if (all_ok == false){
alert("The following errors have taken place" + err_msg);
setFocus(fld);
}
return all_ok;
}
答案 0 :(得分:1)
令人遗憾的是,计算机按照我们的要求行事,而不是我们想要的。
如果您的输入中填充了0000,并且您将<
与1
:($("#couponstart1").val() < 1
进行了比较,则javascript会尝试将值0000
解析为操作数帐户的类型为0
。所以说0 is lower than 1
是正确的,你会得到错误信息Card number are not correct
。
让我们尝试一种不同的方法:
<!-- html -->
<form>
<p>
<label for="couponstart1">Coupon Start Part 1</label>
<input type="text" maxlength="4" id="couponstart1" class="validateme" />
</p>
<p>
<label for="couponstart2">Coupon Start Part 2</label>
<input type="text" maxlength="4" id="couponstart2" class="validateme" />
</p>
<p>
<label for="couponend1">Coupon End Part 1</label>
<input type="text" maxlength="4" id="couponend1" class="validateme" />
</p>
<p>
<label for="couponstart2">Coupon Start End 2</label>
<input type="text" maxlength="4" id="couponend2" class="validateme" />
</p>
<button type="button" id="testit">Test</button>
</form>
和
/* javascript/jQuery */
$(document).ready(function(){
$("#testit").click(function(){
var rgx = /\d{4}/;
var allgood = true;
$("input.validateme").each(function(){
if( ! rgx.test( $(this).val() ) ){
alert("Card number for " + ($('label[for="' + $(this).attr("id") + '"]').text()) + " is not correct!\nPlease use 4 digits all numbers!");
$(this).select();
allgood = false;
return false;
}
});
if(allgood) alert("All good!");
});
});
基本上我们有一个包含4 inputs
和“validateme”class
的表单。
使用jQuery,我们循环遍历具有此类的输入,并针对正则表达式/\d{4}/
运行它,它基本上将测试一个数字(\d
)正好4位数字({4}
),意思是0000请注意,我们使用输入的标签来识别哪个字段不正确。
否则,一切都好。
您可以在此处使用此代码:http://jsfiddle.net/L8dcgcrs/1/
答案 1 :(得分:0)
假设四个数字对应$("#couponstart1").val(), $("#couponstart2").val(), $("#couponend1").val(), $("#couponend2").val()
,那么问题是在JavaScript中有两种类型的比较运算符。
在您的第一个if
语句中,您选择评估if $("#coupontstart1").val() == '' || ...)
。在这种情况下,您使用的是等于运算符,如果它们不是同一类型,则转换操作数。因此,使用此运算符0 == ''
会返回true
。相反,您可以通过以下方式使用严格相等运算符===
:
if ( $("#couponstart1").val() === '' || ...)
在这种情况下,0 === ''
将返回false。这很可能会帮助您在写入的第一个if语句中输入0000
会使所有条件都为false并且您将继续执行else块。
希望这有帮助!