2

I have a form where I am calling the valid() function on each form element separately. I have an field named "altemailaddress" that I set a rule to be "email" and required is false. However the valid() function returns false if there is no value in the input text box.

jQuery("#aspnet-form").validate({
    onsubmit: false,
    rules: {
        prefix: "required",
        emailaddress: {
            required: true,
            email: true
        },
        altemailaddress: {
            required: false,
            email: true
        }

    },
    messages: {
        prefix: "Please enter a prefix",
        emailaddress: {
            required: "Please enter an email address",
            email: "Invalid email format"
        }

    }
});

var $group = jQuery(this).parents(".validationGroup");
    var isValid = true;


    $group.find(":input").each(function (i, item) {
        if (!jQuery(item).valid())
        {

            isValid = false;
        }           

    });

As I loop through each item, I call the valid() function. If the rule for altemailaddress says require: false, it ignores it and returns false if no value is provided.

If I just set up the validate plugin to validate on submit it works fine.

2 Answers 2

4

I think valid() is only for an enitre form

Try using $.validate().element(item)

if (!$("#aspnet-form").validate().element( item ))
{
  isValid = false;
}  

http://docs.jquery.com/Plugins/Validation/Validator/element#element

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

2 Comments

This worked at first, then without changing any code, I tested again and now it doesn't work. I will have to try again
make sure you are still using jQuery 1.3 I had some sites start failing yesterday because they automatically started using jQuery 1.4, and some of the plugins didnt work
0

I had the same problem, my solution is a bit longer but works fine:

Let's say you have an input field "Email" like :

<input id="Email" class="text-box single-line" type="text" value="" name="Email" data-val-regex-pattern="[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[a-zA-Z]{2,4}" data-val-regex="Invalid email address." data-val="true"></input>

You can check if the value of the field matches the regex like this:

var emailreg = $('#Email').attr('data-val-regex-pattern');
if($('#Email').val().match(emailreg) != null){ //the field is valid }

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.