开发者

Checking password with server and jquery Validate

$("#changePassword").validate({
        rules: {    
            newPassword: {
                required: true              
            },
            newPassword2: {
                required: true,             
       开发者_StackOverflow社区         equalTo: "#newPassword"
            }               
        }
    });

Hey guys this is my validation code.

And this is my password check

    $('#oldPassword').change(function() {
        var password = $(this).val();
        $.post('/settings/check/oldPassword', { value : password }, function(data) {

            if (data == 'false') {                  
                $('#oldPassword').removeClass('valid');
                $('#oldPassword').addClass('error');
            }

        });
    });

How do I make it so the form will not submit when it has this error. I also thought jquery validation didn't submit if a form element had the class error but apparently not. The error class is getting added fine but you can still submit the form. Thanks for any help.

EDIT

Here is the form.

        <input type="text" name="oldPassword" placeholder="Old Password" value="" title="Old Password" class="hint" id="oldPassword" size="30"  /> 

    <br />
    <br />
    <input type="password" name="newPassword" placeholder="New Password" value="" title="New Password" class="hint" id="newPassword" size="30"  /> 

    <br />
    <br />

    <input type="password" name="newPassword2" placeholder="Verify Password" value="" title="Verify Password" class="hint" id="newPassword2" size="30"  /> 

    <br />
    <br />
    <input type="submit" id="submitForm" value="Change Password" />


You could do:

$('#oldPassword').submit(function() {
    var password = $(this).val();
    $.post('/settings/check/oldPassword', { value : password }, function(data) {

        if (data == 'false') {                  
            $('#oldPassword').removeClass('valid');
            $('#oldPassword').addClass('error');
            return false;
        }

    });
});

EDIT - if you need to valiate a value of an input by checking a remote resource you should use remote() , you definitly should not try to "confuse" the plugin by thinking that the form is invalid


You're going to have to use .submit() and not change() and return false if validation failed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜