Jump to content

Recommended Posts

Just wanted to ask that with jquery's validation plugin can be used in the following manner. Meaning am I using it correctly. Also with the way my code is structured if there are errors will it still submit or is there an additional step I need to do like some sort of if statement that if there's no errors then submit.

 

<script type="text/javascript">
$(document).ready(function() {
    $('div.message-error').hide();
    $('div.message-success').hide();
    $('ul#characterList').css( 'margin-left', '120px' );
    $('li').remove('.characterName');
    $("#handlerForm").validate({ 
        rules: {
            firstName: "required",
            lastName: "required",
            userName: {
                required: true,
                minlength: 5
            },
            password: {
                required: true,
                minlength: 5
            },
            password2: {
                required: true,
                minlength: 5,
                equalTo: "#password"
            },
            email: {
                required: true,
                email: true
            },
        },
        messages: {
            firstName: "Please enter your firstname",
            lastName: "Please enter your lastname",
            userName: {
                required: "Please enter a username",
                minlength: "Your username must consist of at least 2 characters"
            },
            password: {
                required: "Please provide a password",
                minlength: "Your password must be at least 5 characters long"
            },
            password2: {
                required: "Please provide a password",
                minlength: "Your password must be at least 5 characters long",
                equalTo: "Please enter the same password as above"
            },
            email: "Please enter a valid email address"
        }
    });
    $("input.submit").click(function() {
        var userID = $("input#userID").val();
        var defaultChar = $("select#charactersDrop option:selected").text();
        var userName = $("input#userName").val();
        var firstName = $("input#firstName").val();
        var lastName = $("input#lastName").val(); 
        var password = $("input#password").val(); 
        var email = $("input#email").val(); 
        var statusID = $("select#statusID").val(); 
        var isAdmin = $("select#isAdmin").val();   
        var liElements = $("ul#characterList li");
        var characterIDList = "";
        for( var i = 0; i < liElements.length; i++ ) {
            var liElement = $( liElements[ i ] );
            
            // only start appending commas in after the first characterID
            if( i > 0 ) {
            characterIDList += ","; 
            }
            
            // append the current li element's characterID to the list
            characterIDList += liElement.data( 'characterID' );
        }
        var dataString = 'userName=' + userName + '&firstName=' + firstName + '&lastName=' + lastName + '&email=' + email + '&statusID=' + statusID + '&isAdmin=' + isAdmin + '&characterIDList=' + characterIDList + '&handlerID=' + handlerID + '&editHandler=True';
        $.ajax({
            type: "POST",
            url: "processes/handler.php",
            data: dataString,
            success: function() {
                $('div.message-error').hide();
                $("div.message-success").html("<h6>Operation successful</h6><p>" + userName + " saved successfully.</p>");
                $("div.message-success").show().delay(10000).hide("slow");
                $(':input','#handlerForm')
                .not(':submit, :button, :hidden')
                .val('')
                return true;
            }
        });
        return false;    
    });
});
</script>

Link to comment
https://forums.phpfreaks.com/topic/230143-validation-plugin/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.