0

I would like to display 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...?

2
  • 1
    Have you tried it? It should work. Commented Aug 26, 2011 at 0:35
  • I did try but for some reason couldn't get it to work for me. Probably some silly mistake, not been doing this for that long. Commented Aug 26, 2011 at 0:39

2 Answers 2

2
function checkValid(){

     var userEntry = document.getElementById("entry1").value;

     if (!isValid(firstRef)) {
         alert("Invalid format try again.");
     }

}
Sign up to request clarification or add additional context in comments.

Comments

1

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");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.