2011-03-11 75 views
0

我想包括这个正则表达式条件并激活下面发布的javascript函数中的分析代码(如果它符合正则表达式条件)。条件语法帮助

if (/^[abc].*/i.test(mystring)) { 

    var _gaq = _gaq || []; 
    _gaq.push(['_setAccount', 'UA-XXXXXXX-XX']); 
    _gaq.push(['_trackPageview']); 
     (function() { 
     var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; 
     ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; 
     var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); 
     })(); 

     alert('Congrats ABC!'); 
} 



现在,我怎么在里面添加上述(正则表达式条件和分析)的代码?
另请注意。下面的部分目前完美。

function validateCoupon(form){ 
    var mystring = document.getElementById('textCoupon').value; 

if(form.textCoupon.value.length) 
     { 
    // validate coupon 
    $.get("/include/checkItOut.php", { coupon: form.textCoupon.value }, validateShipping); 
    alert('Performed the check and matched'); 
     } 
else 
{ 
    form.textCoupon.style.border = ''; 
    validateShipping("yes"); 

    alert('You Did Not Use Any Code'); 
} 

return false; 
} 


换句话说。以某种方式将Regex条件和分析触发器包含在这里。

+5

什么“现在我该如何添加上面的代码?”意思? – 2011-03-11 16:50:42

+0

我试图在下面的代码中包含这个部分if(/^abc$/i.test ...没有成功,它可能是一个语法或条件问题,对此有什么建议吗? – detonate 2011-03-11 17:00:12

+1

在'if(/^abc $/i.test(mystring);){',从'if()'中移除';'。 – 2011-03-11 17:05:15

回答

0

使用等号运算符(==)的优惠券代码(小写)和abc之间的字面比较就足够了。您应该考虑服务器端解决方案,每个人都可以打开源代码并从中获取优惠券代码“abc”。

function validateCoupon(form){ 
    var mystring = document.getElementById('textCoupon').value; 

if(form.textCoupon.value.length) 
     { 
    // added: Analytics 
    if (form.textCoupon.value.toLowerCase() == "abc") { 
     var _gaq = _gaq || []; 
     _gaq.push(['_setAccount', 'UA-XXXXXXX-XX']); 
     _gaq.push(['_trackPageview']); 
     (function() { 
     var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; 
     ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; 
     var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); 
     })(); 
    } 
    // end analytics 
    // validate coupon 
    $.get("/include/checkItOut.php", { coupon: form.textCoupon.value }, validateShipping); 
    alert('Performed the check and matched'); 
     } 
else 
{ 
    form.textCoupon.style.border = ''; 
    validateShipping("yes"); 

    alert('You Did Not Use Any Code'); 
} 

return false; 
} 
+0

再一次,被我的头撞在语法上,现在它就像一个魅力Lekensteyn!我是一名设计师,我能否以任何方式帮助你? – detonate 2011-03-11 19:55:04

+0

但是,只有一个问题,分析是否每次都采取行动,或者仅当abc在此答案中正确回答时才会采取行动? – detonate 2011-03-11 19:56:42

+0

@detonate:如果优惠券代码等于'abc'(不区分大小写),则会调用分析。再一次,这种方法*不安全*。它看起来像Javascript中的“登录系统”的noobish实现:'if(password ==“apple”)/ * ACCESS GRANTED * /'。 (可以在源代码中找到)即使使用某种哈希,由于优惠券代码通常不会很长,因此优惠券代码仍然可能被强制使用。但是,如果您不关心优惠券代码是否可见,则此代码无问题。 – Lekensteyn 2011-03-11 20:40:09