1

I have a small problem with a task I've been asigned. I'm trying to make an alert message appear if the length of the inputted number does not equal 7. The message appears even if the length of the number is equal to 7 and I can't figure out why, any help would be appreciated! thanks.

var msg = "";

if (document.Entry.Number.length!== 7) {
            msg+="Your Number should be 7 digits. Please check this. \n";
            document.Entry.Number.focus();
            document.getElementById('Number').style.color="red";
            result = false;
        }
        if(msg==""){
            return result;
        }

        {
            alert(msg)
            return result;
        }
0

2 Answers 2

4

You can use document.Entry.Number.value.length in the if condition,

var msg = "";

if (document.Entry.Number.value.length!== 7) {
            msg+="Your Number should be 7 digits. Please check this. \n";
            document.Entry.nNumber.focus();
            document.getElementById('Number').style.color="red";
            result = false;
        }
        if(msg==""){
            return result;
        }

        {
            alert(msg)
            return result;
        }
Sign up to request clarification or add additional context in comments.

1 Comment

got it to work just by adding a .value before the length in the original script. Thanks for your help!
0

That should work:

if (document.Entry.Number.toString().length!== 7) {

If document.Entry.Number is a number, you'll have to convert it to a string to find out the length. (Ref Length of Number in JavaScript)

2 Comments

Thank you for the quick reply! I'll remember that in future. However I'm still receiving the alert after editing that. Hmm, any more suggestions?
Then alert document.Entry.Number or try trim()

Your Answer

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