1

I have the following validation on a form field:

$(".controlsphone input").blur(function() {
    var ree = /^\d+$/;
    if(ree.test(document.getElementById("register_telephone").value))
        {
            $(this).css('border-color','green');
            $(this).siblings('.info').css('display','none');
            $(this).siblings('.error').css('display','none');
            $(this).siblings('.valid').css('display','inline-block');   
            $("#email_error401112").hide();
            $('#registerErrors').hide();
    }
    else
    {
        $('#registerErrors').show();
        $('#email_error401112').remove();
        $('#registerErrors').append('<p id="email_error401112">Please enter a phone number</p>');
    }
});

I would like to only validate the field if a number exists. The field is not required, but if there is content within the field, it needs to be valid (a number)

How does the above code look? Any ideas what i can do to implement this?

Cheers, Dan

1

1 Answer 1

3

Use

var ree = /^\d*$/;

because + stands for one or more, excluding zero.

while * stands for zero or more

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

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.