3
votes

Experts I need little help here. I have a jsp page where three validation will done:

  1. Password Strength (using Live Checking, Not validate() function)
  2. Confirm Password (using validate() function, Not Live Checking)
  3. Email Validation (using Live Checking, Not validate() function)

Live checking is done by keyUp() function and corresponding jQuery here:

 $(document).ready(function(){

 $("#register-form").validate({
 rules: {
   password_again: {
      equalTo: "#password"
        }
                    }
 });


 $("#validate").keyup(function(){

        var email = $("#validate").val();

        if(email != 0)
        {
            if(isValidEmailAddress(email))
            {
                $("#validEmail").css({
                    "background-image": "url('validate/validYes.png')"
                });
            } else {
                $("#validEmail").css({
                    "background-image": "url('validate/validNo.png')"
                });
            }
        } else {
            $("#validEmail").css({
                "background-image": "none"
            });         
        }

    });

            $('#password').keyup(function(){
    $('#result').html(checkStrength($('#password').val()));
}); 

function checkStrength(password){

//initial strength
var strength = 0;

//if the password length is less than 6, return message.
if (password.length < 6) { 
    $('#result').removeClass();
    $('#result').addClass('short');
    return 'SHORT';
}

//length is ok, lets continue.

//if length is 8 characters or more, increase strength value
if (password.length > 7) strength += 1;

//if password contains both lower and uppercase characters, increase strength value
if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))  strength += 1;

//if it has numbers and characters, increase strength value
if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/))  strength += 1;

//if it has one special character, increase strength value
    if (password.match(/([!,%,&,@,#,$,^,*,?,_,~])/))  strength += 1;

//if it has two special characters, increase strength value
    if (password.match(/(.*[!,%,&,@,#,$,^,*,?,_,~].*[!,%,&,@,#,$,^,*,?,_,~])/)) strength += 1;

//now we have calculated strength value, we can return messages

//if value is less than 2
if (strength < 2 ) {
    $('#result').removeClass();
    $('#result').addClass('weak');
    return 'WEAK';  
} else if (strength == 2 ) {
    $('#result').removeClass();
    $('#result').addClass('good');
    return 'GOOD';
} else {
    $('#result').removeClass();
    $('#result').addClass('strong');
    return 'STRONG';
}
}


});

function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
}

All is working fine but I cannot prevent the page forwarding when any of those three validation(Confirm_Password, Password_Strength, Email) is failed after hit the Submit Button except the Confirm_Password validation. I want to restrict the action for the Email & Password_Strength too. Means whenever a validation is failed the set Focus to that field. Notice that validate() function used only for Confirm_Password.

2
For whatever reason you've decided to do the most complicated form validation routines outside of the jQuery Validate plugin. It seems very pointless to use the jQuery Validate plugin for the most basic task of checking that one field is equal to another. Might as well go the extra little bit and not use jQuery Validate plugin at all. - Sparky
Actually I need to check Email_Pattern & Password_Strength live.For this i made it this way( outside the validate()).Any suggestion. - Somnath Kayal
Yes, my suggestion is to not complicate this any further by using jQuery Validate for something as simple as comparing two fields. - Sparky
Comparing two fields?? I compare two fields in the case of confirm_passwd, but in case if Email_Pattern & Password_Strength how we can compare two fields?? Check this: htmldrive.net/items/show/823/… - Somnath Kayal
What part are you not understanding? jQuery Validate is a powerful plugin and, for the most part, you aren't even using it. You're bypassing it for the more complex tasks. You might as well just confirm the two password fields are equal (compare two fields) with some jQuery inside your keyup handler, and then since you're mostly not using it, remove the jQuery Validate plugin entirely. - Sparky

2 Answers

0
votes

You haven't specified the JSF implementation/library you are using but assuming that you want to do many things in JavaScript, and in your case you want to do Javascript validation only, I recommend:

1) Use PrimeFaces

2) use the p:remoteCommand instead of the action from button

3) in p:commandButton use onclick instead of action, in which you will call the function that will do the validation, and call the remote command only if the validation is successful.

0
votes

Assuming that your question is how to prevent the processing of data with jQuery if you see/know that they are not good with jQuery itself, I would suggest using the $.ajax function. With this function, you can choose to send a request only if everything goes well. An example might be this:

$(document).ready(function(){
    // Here your controls about password strenght, email, etc...
    $('#FORM-ID').submit(function(){
        var url = "YOUR_ACTION";

        // YOUR CONTROLS
        if( isOK() ) {
            $.ajax({
                type: "POST",
                url: url,
                // You can use any other way to pass the data,
                //    my favorite is to serialize with jQuery function
                data: $("#FORM-ID").serialize(),
                success: function(data){
                     // Success Message
                } else {
                     alert(data);
                }
             });
             return false;
        } else {
             // Display Error message
        } 
    });
});

You can find a pseudo-example here, while here you can find the official documentation for $.ajax. Let me know if I hit the problem ;)

You can write a simple boolean function for controls like this:

function isOk(parameters_to_check){
    if(all_conditions_are_true){
        return true;
    } else {
        return false;
    }