2

I'm looking Jquery UI's modal form demo. But i want to add something. This demo checking all inputs' (name,email,password) lenght and validate them. But email input must be optional. Because of this i'm setting email input's min value (for checkLength() function) to zero. But when i leave blank email input i'm getting validation error. You can check full codes here .

It's validation email with this code :

bValid = bValid && checkRegexp( email, ..SOME REGEX PATTERNS WILL COME HERE.., "eg. [email protected]" );

But i want to try validation only if there is any email input. Because email input is optional and visitors can leave it. How can i add if statement (if email input is filled, validate it. Else don't try validation) to this code ? I hope it's clear.

1 Answer 1

1

If you are saying "how to do validation only if email is not empty?", then I see no reason this wouldn't work:

var email = $( "#email" ); // taken from the linked code
if (email.val() != "") { // e-mail field value is not empty
  bValid = bValid && checkRegexp( email, ..SOME REGEX PATTERNS WILL COME HERE.., "eg. [email protected]" );
}

Note: if you actually have no idea how to use conditional statements in JS, you may have a lot of hurdles ahead of you. See this for a quick reference, and/or get a book about JS basics.

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

2 Comments

i'm trying to use email.lenght === 0 condition but it's not work. I'll try this. thank you.
@Eray: You're welcome. Where did you get that syntax? First, lenght is probably a typo; second, this would get you a number of elements matching #email - see the documentation.

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.