Hi I am trying to validate a phone number for a cookie via java-script. The prompt works great but the validation does not. This is for a blackberry webworks app.
I read other posts but I am missing something
function checkCookie()
{
var username=getCookie("username");
if (username!=null && username!="")
{
开发者_JAVA技巧 //alert("Your Phone Number is " + username);
}
else
{
username=prompt("When asked you can receive texted coupons. Please enter your Phone Number:","");
if (username!=null && username!="")
{
function validatePhone(username)
{
if (field.match(/^\d{10}/)) {
setCookie("username",username,365);
return true;
}
alert("wrong");
return false;
}
}
}
}
I don't see an actual call to the validatePhone function. You've defined it, but never call it.
Try this instead:
function validatePhone(sInput) {
return /^\d{10}$/.test(sInput);
}
function checkCookie() {
var username=getCookie("username");
if(username!=null && username!="") {
// alert("Your Phone Number is " + username);
} else {
username=prompt("When asked you can receive texted coupons. Please enter your Phone Number:","");
if(username!=null && username!="") {
var bValid = validatePhone(username);
if(bValid) {
setCookie("username",username,365);
return true;
} else {
alert('wrong');
return false;
}
}
}
}
Edit: I switched the validation function to use RegExp.test instead of String.match.
精彩评论