Javascript 条件语法帮助
我试图在下面发布的javascript函数中包含这个正则表达式条件,如果它满足正则表达式条件,则激活分析代码Javascript 条件语法帮助,javascript,Javascript,我试图在下面发布的javascript函数中包含这个正则表达式条件,如果它满足正则表达式条件,则激活分析代码 if (/^[abc].*/i.test(mystring)) { var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-XXXXXXX-XX']); _gaq.push(['_trackPageview']); (function() { var ga = docume
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!');
}
现在,我如何在regex条件和分析上面添加代码?另外,请注意。下面的部分目前运行良好
换句话说。以某种方式将Regex条件和分析触发器包含在此函数中。在小写优惠券代码和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;
}
现在该怎么做?我怎样才能在那里添加上面的代码?是什么意思?我正在尝试将/^abc$/I.test。。。在下面的代码中没有成功。这可能是语法或条件问题。对此有何建议?在if/^abc$/i.testmystring;{,从if内部移除;/^[abc].*/i.testmystring这样更改了它。它需要检查并确保它与ABC匹配。这是否正确?您真正想要的是什么?如果调用ValidateCoon,请调用分析代码?mystring不包含在下面的函数中,它应该做什么?将其与优惠券代码进行匹配?再一次,我被发现在语法上撞了头,现在这是w我是一名设计师,我能以任何方式回报你吗?只有一个问题,分析是每次都会采取行动,还是只有在abc在这个答案中得到正确回答时才会采取行动?@雷管:如果couponcode等于abc不区分大小写,将调用分析。再一次,这种方法是不安全的。它看起来像noobishJavascript中登录系统的实现:ifpassword==apple/*ACCESS-grated*/。即使使用某种散列,也可以在源代码中找到它,优惠券代码仍然可以强制执行,因为优惠券代码通常不太长。但是,如果您不关心优惠券代码是否可见,则此代码很好。很好的建议ion!可能会研究MD5之类的。谢谢你的帮助Lekensteyn!
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;
}