I would like to displa开发者_C百科y a custom alert, rather than simply true or false. I have:
function isValid(test) {
return /^[a-zA-Z]{2}(?:\d{6}|\d{8}|\d{10})$/.test(test);
}
function checkValid(){
var userEntry = document.getElementById("entry1").value;
alert(isValid(firstRef));
}
So depending on whether what the user is valid or not they get the message "true" or "false". I would like the user to get a customised message if their input returns false such as "Invalid format try again" and get no message displayed when they input the correct data. Could I somehow use an if statement along the lines of if true then.... else...?
function checkValid(){
var userEntry = document.getElementById("entry1").value;
if (!isValid(firstRef)) {
alert("Invalid format try again.");
}
}
Change your checkValid function to something like this one:
function checkValid(){
var userEntry = document.getElementById("entry1").value;
if (isValid(firstRef)) {
alert("It is valid!");
}
else {
alert("It is invalid");
}
}
精彩评论