0

I am fairly new to jQuery validation http://validation.bassistance.de/ and am trying to create a custom event using addMethod. I have coded what appears to look correct but obviously isn't because if the event is true, then #email should be hidden with no message.

What is happening is that if the event is true or false, the message 'Please enter a value for testing!' is always fired. I have obviously made an error of epic proportions but cannot seem to find a way to correct it. I would be grateful if someone could point out my error. I have only posted the relevant piece of code. Thanks

Fiddle code: http://jsfiddle.net/DQjTc/

UPDATE: Just an update. I have noticed that if the submit button is pushed after validation, the correct message is entered and the false message does not. Which is what I need. Where do we go from here.

addMethod code

    $(function() {

    $.validator.addMethod( 'emailHide', function(value, element) 
    {

        if (element.value === '')

            {

                return false;
            }


                //return true;
                $('#email').hide();
                $('#emaillbl').html('Email address approved');


        },'Please enter a value for testing!'


    );
});

validation rules code

    $(function () {

    $.validator.setDefaults(
    {
        errorClass: 'form_error_frmreport',
        errorElement: 'div'
    });

    $("#frmreport").validate(
    {
        rules:
        {

            email:
            {
                //required: true,
                email: true,
                emailHide: true
            },
            position:
            {
                required: true
            },
            feedback:
            {
                required: true
            }
        },
        messages:
        {


            email:
            {
                //required: "<br />* required: You must enter a valid email address"
            },
            position:
            {
                required: "<br />* required: Please state your position"
            },
            feedback:
            {
                required: "<br />* required: Please enter as much information regarding the exact nature of the problem"
            }
        },

        submitHandler: function()   {
                if ($("#frmreport").valid() === true)  { 
                var data = $("#frmreport").serialize();
                $.post('/sample/admin/frm10010.php', data, function(msg) {

               var messageOutput = '';
                for (var i = 0; i<msg.length; i++){
                    messageOutput += msg[i].box+'  ';     
                }
                $("#confirm_department").hide();


               var $dialog = $('<div id="dialog"></div>')
               .html('Your report was successfully submitted and a representative will respond to you shortly.<br /><br />Thank you.');
               $dialog.dialog({
               autoOpen: true,
               modal: true,
               title: 'Report submission successfull',
               width: 400,
               height: 200,
               draggable: false,
               resizable: false,
               buttons: {
               Close: function() {
               $( this ).dialog( "close" );
               }
               }
               });
                $("#frmreport").get(0).reset();
                }, 'json');

         } else

         { 
           return; 
         }
        },
        success:  function()   {

                //validator.resetForm();
                //$.html("You have entered a box");
                //$("#BA_boxform").get(0).reset();
        }   

    });
});
4
  • try replacing === with a == Commented Jul 12, 2013 at 9:41
  • @iBlue still the same error. thanks Commented Jul 12, 2013 at 9:47
  • can u post ur full code including html or a jsfiddle Commented Jul 12, 2013 at 9:51
  • iBlue jsfiddle.net/DQjTc ignore the submit and php values. thanks Commented Jul 12, 2013 at 9:53

2 Answers 2

1

Not sure but try this replace if (element.value ==='') ** with **if (value ==='')

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

1 Comment

Can you try this... Need to add return true in ELSE condition if (element.value ===''){ return false; } else { $('#email').hide(); return true;}
1

The value may contain white-space, so you should try:

if (element.value.trim() == '')

Not all browsers support trim, so you should place this at the top of your Javascript file.

if(!String.prototype.trim) {
  String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g,'');
  };
}

1 Comment

Mr.Polywhirl still the same result. Thanks

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.