使用javascript或jquery的优惠券代码验证功能

时间:2016-08-10 06:00:50

标签: javascript jquery html



function validate(coupon) {
    var myRe = "LUCKY100";
    if(cvv.value.match(myRe)) {
        return true;
    } else {
        document.getElementById('couponerror').innerHTML="Invalid coupon";
        return false;
    }
} 

<form onsubmit="validate(coupon)">
<label style="width:50px;"><input type="text" name="coupon" class="coupon" title="Enter coupon" o >
</p><span id="usernameError"></span></label>
<input type="button"  value="Submit" />
&#13;
&#13;
&#13;

我是jquery的新手我的问题是我需要一个代码验证方法,比如我分配一个LUCKY100&amp;的优惠券代码。如果用户输入输入类型的不同值,则会在下面给出错误信息输入字段而不是警报&amp;如果用户输入LUCKY100,则会成功应用消息优惠券。请提供此验证的html和javascript或jquery。 提前致谢

2 个答案:

答案 0 :(得分:0)

根据您的代码,正确的值是硬编码的。假设您需要不区分大小写的检查:

  1. 将输入元素添加为:

  2. 并在你的js:

    id         date              value   
    ---       -----------        --------- 
    1          2016-05-01        234  
    2          2016-05-02        567  
    3          2016-05-03        800
    4          2016-05-03        300  
    

答案 1 :(得分:0)

试试这个,

&#13;
&#13;
function validate(coupon) {
    var myRe = "LUCKY100";
    var coupon = myRe.trim();
    var input = document.getElementById('in').value;
    if(input.toUpperCase() == coupon.toUpperCase()) {
        document.getElementById('message').innerHTML="Coupon applied!";
        document.getElementById('err').innerHTML="";
        return true;
    } else {
        document.getElementById('err').innerHTML="Invalid coupon";
        document.getElementById('message').innerHTML="";
        return false;
    }
}
&#13;
#message{
  position: relative;
  top: 10px;
  color:green;
}
#err{
  position: relative;
  top: 10px;
  color:red;
}
&#13;
<form onsubmit="">
<label style="width:50px;"><input type="text" name="coupon" id="in" class="coupon" title="Enter coupon" >
<span id="usernameError"></span></label>
<input type="button" value="Submit" onClick="validate(coupon)" /></form>

<span id="message"></span>
<span id="err"></span>
&#13;
&#13;
&#13;

希望有所帮助!