jquery form validation plugin issue : Please help
in Using jQuery Plugins
•
11 months ago
Hi All
I'm new for Jquery, and im try to impliment Jquery Form Validation plugin into a form.
During my testing, I have several question about using form validation plugin
Here is my form code :
- <script src="#application.http#jquery/lib/jquery.js" type="text/javascript"></script>
- <script src="#application.http#jquery/jquery.validate.js" type="text/javascript"></script>
- $.validator.setDefaults({
- debug: true,
- submitHandler: function() {
- $("#btnUpdate").click(function() {
- alert("submitHandler:btnUpdate");
- if (confirm("Are you sure to Update the Record")){
- alert("click Yes");
- }else{
- alert("click Cancel");
- };
- });
- $("#btnCancel").click(function() {
- alert("submitHandler:btnCancel");
- });
- var f = document.signupForm;
- alert("submitHandler:submitted!");
- }
- });
- $().ready(function() { //1-1
- $("#btnUpdate").click(function() {
- alert("btnUpdate");
- $("#signupForm").validate({ //2
- rules: {
- firstname: {
- required: true,
- minlength: 5
- //remove_show_msg: "firstname"
- }
- },
- messages: {
- firstname : "<table bgcolor='F5EFC9' border='1' bordercolor='ffffff'><tr><td><font color='red'><b>Please enter your firstname update 2</b><font></td></tr></table>"
- },
- // the errorPlacement has to take the table layout into account
- errorPlacement: function(error, element) { //4-1
- //Version 2
- //alert(element.attr("name"));
- jsSetIDName = $('#' + element.attr("name") + '_msg').fadeIn();
- error.appendTo(jsSetIDName);
- // End Version 2
- }//4-1
- });//2
- });
- $("#btnCancel").click(function() {
- alert("btnCancel");
- $("#show_msg").html("");
- var validator2 = $("#signupForm").validate();
- validator2.form({
- rules: {
- comment: {
- required: true,
- minlength: 5
- //remove_show_msg: "firstname"
- }
- },
- messages: {
- comment : "<table bgcolor='F5EFC9' border='1' bordercolor='ffffff'><tr><td><font color='red'><b>Please enter your firstname update 2</b><font></td></tr></table>"
- },
- // the errorPlacement has to take the table layout into account
- errorPlacement: function(error, element) { //4-1
- //Version 2
- //alert(element.attr("name"));
- jsSetIDName = $('#' + element.attr("name") + '_msg').fadeIn();
- error.appendTo(jsSetIDName);
- // End Version 2
- }//4-1
- });
- });
- }); //1-1
- </script>
- </head>
- <body>
- <h1 id="banner"><a href="http://bassistance.de/jquery-plugins/jquery-plugin-validation/">jQuery Validation Plugin</a> Demo</h1>
- <div id="main">
- <p>Default submitHandler is set to display an alert into of submitting the form</p>
- <form id="signupForm" method="post" action="">
- <fieldset>
- <legend>Validating a complete form</legend>
- <table border="0" width="700">
- <tr>
- <td align="right" valign="top" width="200"><label for="firstname">Firstname</label></td>
- <td align="left" valign="top" width="400"><input id="firstname" name="firstname" /><br>
- <span id="firstname_msg" name="firstname_msg" class="show_msg">test</span>
- </td>
- </tr>
- <tr>
- <td align="right" valign="top" width="200"><label for="firstname">Comment</label></td>
- <td align="left" valign="top" width="400"><input id="comment" name="comment" /><br>
- <span id="comment_msg" name="comment_msg" class="show_msg">test</span>
- </td>
- </tr>
- <tr>
- <td colspan="2">
- <input type="submit" name="btnUpdate" id="btnUpdate" value="update" size="20">
- <input type="submit" name="btnSubmit" id="btnSubmit" value="submit" size="20">
- <input type="submit" name="btnCancel" id="btnCancel" value="cancel" size="20">
- </td>
- </tr>
- </table>
- </fieldset>
- </form>
Below is the result i want to achieved :
I have a form, it have 3 action button :
a) Update Record.
b) Submit Record
c) Cancel Record
a) Update record.
-- Clear all the error message.
-- Validated all the form field and update the record,
-- If validation pass, the form will submit and update the record
Action page : Update Record
If Update successfully :
showed alert message to informed user the form record is updated then redirect to main page.
Else show alert Error message.
b) Submit Record.
-- It will validated all the form field
-- Once validate pass, it will show an alert message : Are you sure you want to submit the record?
-- Yes = Submit the record for process,
-- If Update successfully : show Alert message : "You record xxx is submitted successfully".
-- Else show alert Error message.
-- No = Do nothing.
c) Cancel Record
-- Clear all the error message.
-- It will make sure the form field REASON textarea need to be entered only..
-- Once the validation pass, alert message showed : Are your sure you want to Cancel?
If yes : Submit form and update the record status to Cancel
Show alert message : "Your XXX record is Cancel".
No : Do nothing.
Issue
1) How to write A and C validation script where by:
-- A is validate all form field and
-- C only Comment text is required?
I try to split the [UPDATE] and [Cancel] button validation script into difference block but no luck.
After i click the [Update] button and then click on the [Cancel] button, It seem it only run the [Update] validation script but not [Cancel] validation script.
I try to used below URL sample (If .. Else) but no luck too :
http://forum.jquery.com/topic/jquery-validate-creditcard-issue
2) How to showe alert message to let user decide the next action by click Yes or No before or after validation?
I try to use below code :
- $("#btnUpdate").click(function() {
- alert("submitHandler:btnUpdate");
- if (confirm("Are you sure to Update the Record")){
- alert("click Yes");
- }else{
- alert("click Cancel");
- };
- });
It seem it need to click [Update] button x 2 time, then the alert message will showed.
It should show if the validation is pass?
-- Submit successfully
-- Return error message when form submition false.
I believe this can be done via using return javascript function.
But the way, is that any jquery script i can refer?
4) To used <input type="button">
So far the sample i found via internet is all using <input type="submit">. Is that any sample using input type ="button"> to control the button action?
Thank for all who can help me to solve this issue. Very appreciated.
Regard
Keong
1



