1

I have a bit of a strange problem. I'm creating a small webapp using JqueryMobile 1.4 and JqueryValidate http://jqueryvalidation.org/ for my form validation. I have a popup that is opened and the user can enter some data and on submit it uses $.post to post the data to the DB. Here is the jquery

 //Matches UK landline + mobile, accepting only 01-3 for landline or 07 for mobile to    exclude many premium numbers
jQuery.validator.addMethod('phonesUK', function(phone_number, element) {
phone_number = phone_number.replace(/\(|\)|\s+|-/g,'');
return this.optional(element) || phone_number.length > 9 &&
phone_number.match(/^(?:(?:(?:00\s?|\+)44\s?|0)(?:1\d{8,9}|[23]\d{9}|7(?:[45789]\d{8}|624\d{6})))$/);
});

jQuery.validator.addMethod("phoneOrEmail", function(value, element) {
return this.optional(element) ||
($.validator.methods["phonesUK"].call(this, value, element)) ||
($.validator.methods["email"].call(this, value, element));
}, "Please enter a valid phone number or email address");

                $().ready(function() {      
                    // validate new number form
                    $("#addNumber").validate({
                        errorPlacement: function(error, element) {
                            error.insertAfter(element.parent()); // <- make sure the error message appears after parent element (after textbox!)
                        },
                        rules: {

                            phoneNumber: 
                                {
                                    phoneOrEmail: true,
                                    required: true,
                                }

                        },
                        messages: {
                            phoneNumber: "Please enter a valid number or email",
                        },

                        submitHandler: function(form) {
                            $('#PopUpAddNumber').popup('close');
                            $.post("customer_AddNewNumber.php", $("#addNumber").serialize(),  function(response)
                            {
                                 LoadCustomerNumber();
                            });
                            $('#addNumber')[0].reset(); //on close reset form
                        }
                    }); //end validate
                }); // end function

Here is the code for the popup:

            <!-- NEW PHONE OR EMAIL POPUP --> 
            <div data-role='popup' id='PopUpAddNumber' data-theme='a' data-overlay-theme='a' data-dismissible='false' style='min-width:300px;'>
                <div data-role='header' data-theme='a'>
                    <h1>Add Number</h1>
                </div>
                <div data-role='main' class='ui-content'>
                    <form id='addNumber' onsubmit="return false;">

                    <input type="hidden" name="cust_id" id="custident" value='<?php echo $custid; ?>' /> 
                    <input type="hidden" name="sess_id" value='<?php echo $sid; ?>' />

                        <div class="ui-field-contain">
                        <label for="phoneType">Type</label>
                            <select name="phoneType" id="phoneType">
                                <?php echo $phoneInnerOptions; ?>
                            </select>
                        </div>
                        <div class="ui-field-contain">
                            <label for="phoneNumber">Number</label>
                            <input type="text" name="phoneNumber" id="phoneNumber" value="">
                        </div> 
                        <div class="ui-field-contain">
                            <label for="primaryNo">Primary Contact</label>
                            <select name="primaryNo" id="primaryNo">
                                <option value="none" id="none" selected></option>
                                <option value="phone" id="phone" >Primary Phone</option>
                                <option value="email" id="email">Primary Email</option>

                            </select>
                        </div> 
                        <div class='ui-grid-a'>
                            <div class='ui-block-a'>
                                <input type='submit' id="submitNum" value='Update' class='ui-btn ui-btn-inline' data-transition='pop' />
                            </div>
                            <div class='ui-block-b'>
                                <a href='#' class='ui-btn' data-rel='back' data-transition='pop' id="addNumberReset">Cancel</a>
                            </div>
                            <div id="success" style="color: black;"></div>
                        </div>
                    </form>
                </div>
            </div>   <!-- /POPUP -->

This is working well, however, if I enter invalid data and press submit the error appears as expected. If I then cancel I expect ALL of my form data to be erased. But if I then re-open the popup the form is clear BUT the error message is still present! Can any of you think of a way round this?

Please see JS Fiddle here: http://jsfiddle.net/6G52Y/

Thanks :)

Janey
  • 1,260
  • 3
  • 17
  • 39

1 Answers1

2

You would need to use an event handler for the reset button. That event would need to clear the same div used as the error placement for the jQuery validation plugin.You may also be able to use this method: http://jqueryvalidation.org/Validator.resetForm.

Ex:

var validator = $( "#myform" ).validate();
validator.resetForm();
Raymond Camden
  • 10,661
  • 3
  • 34
  • 68
  • Adding simple code is better in case the link get broken in the future. – Omar Mar 26 '14 at 11:39
  • Sigh, I hate that aspect of SO. ;) – Raymond Camden Mar 26 '14 at 11:56
  • 1
    do you hate it now? ;) – Omar Mar 26 '14 at 11:58
  • Hello I tried that and it didn't make any difference.. did this: submitHandler: function(form) { $('#PopUpAddNumber').popup('close'); $.post("customer_AddNewNumber.php", $("#addNumber").serialize(), function(response) { LoadCustomerNumber(); }); //$('#addNumber')[0].reset(); //on close reset form } }); //end validate var validator = $( "#addNumber" ).validate(); validator.resetForm(); }); // end function am I doing this wrong or do you have any other ideas? – Janey Mar 26 '14 at 11:58
  • 1
    @Janey it's working. Wrap all your code in `pagecreate` not `.ready()` http://jsfiddle.net/Palestinian/6G52Y/2/ – Omar Mar 26 '14 at 12:03
  • Ah I see! Why the page create, what is this doing differently if you don't mind me asking? Thanks for your help :) – Janey Mar 26 '14 at 12:10
  • it's equivalent to `.ready()` however `.ready()` should be used in jQM. check [this](http://stackoverflow.com/a/22631520/1771795) and [this](http://stackoverflow.com/a/20459433/1771795). – Omar Mar 26 '14 at 12:15
  • @RaymondCamden I'm sure you _love_ that SO's aspect now =) – Omar Mar 26 '14 at 12:16