question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

validation is working when i click submit but the form is also adding when error show

See original GitHub issue

when i click the button the error messages are showing but its also adding empty values to database.

<script>
$(document)
                .ready(function() {
                    $('.ui.form').form({
                                inline: true,
                                on:  'blur',
                                fields: {
                                    code: {
                                        identifier  : 'code',
                                        rules: [
                                            {
                                                type   : 'empty',
                                                prompt : 'Please enter course code'
                                            }
                                        ]
                                    },
                                     section: {
                                        identifier  : 'section',
                                        rules: [
                                            {
                                                type   : 'empty',
                                                prompt : 'Please enter section'
                                            },
                                             {
                                                type   : 'integer',
                                                prompt : 'Please enter integer number'
                                            }
                                        ]
                                    },
                                     coursename: {
                                        identifier  : 'coursename',
                                        rules: [
                                            {
                                                type   : 'empty',
                                                prompt : 'Please enter coursename'
                                            }
                                        ]
                                    },
                                      totalduration: {
                                        identifier  : 'totalduration',
                                        rules: [
                                            {
                                                type   : 'empty',
                                                prompt : 'Please enter totalduration'
                                            },
                                            {
                                                type   : 'integer',
                                                prompt : 'Please enter integer number'
                                            }
                                        ]
                                    }
                                    
                                }

                            })

                });
</script>

this part is ajax part of buttons onclick

$("#savecourse").on("click", function() { 
      var code = $("#code").val();
      var section = $("#section").val();
      var coursename = $("#coursename").val();
       var totalduration = $("#totalduration").val();
    $.ajax({
      type: "POST",
      url: "course_add.php",
      data: ({coursecode:code,coursesection:section,ccoursename:coursename,duration:totalduration}),
      success: function(data) {
        window.alert("course added successfully.");
        window.location.reload(true);
      }
    });  
});

and this part is the form.

   <form class="ui form">
              <div class="two fields">
                <div class="field">
                  <label>Course Code</label>
                  <input name="code" id="code" type="text">
                </div>
                <div class="field">
                  <label>Section</label>
                  <input name="section" id="section" type="text" >
                </div>
              </div>
              <div class="two fields">
                <div class="field">
                  <label>Course Name</label>
                  <input name="coursename" id="coursename" type="text" >
                </div>
                <div class="field">
                  <label>Total Duration</label>
                  <input name="totalduration" id="totalduration" type="text">
                </div>
              </div>
              
              <button class="ui submit button" id="savecourse">Save Course</button>
              <div class="ui error message"></div>
    </form>

i tried making div the buttons but doesnt change anything.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
awgvcommented, Feb 8, 2017

Hi @cemezgn,

You can do something like that in your click event:

$('#savecourse').on('click', function() { 
  if ( $('.ui.form').form('is valid') ) {
    // Do an AJAX call.
  }
})

The following is a standard reply.

Thank you for posting, but although it’s a valid usage question, we’ve limited GitHub Issues to bug reports and feature requests, keeping the board more manageable for maintainers; see the contributing guidelines for more information on what kind of posts should find themselves into the GitHub Issues board.

To get answers or feedback that might allow you to repost this issue, please use one of our other support resources:

1reaction
ardamose123commented, Dec 21, 2016

The problem is caused by your “click” event on the button. It’s not validating anything; it’s just grabbing all the form’s values and sending them using AJAX.

You can check the validity of the form’s values by using the behaviors described in the documentation: http://semantic-ui.com/behaviors/form.html#/settings

Read more comments on GitHub >

github_iconTop Results From Across the Web

Form Validation Works But Still Submits When User Input The ...
My problem is that the validation works fine: Users get an alert that the phone number they provided has been used but my...
Read more >
Client-side form validation - Learn web development | MDN
Before submitting data to the server, it is important to ensure all required form controls are filled out, in the correct format. This...
Read more >
Building a template-driven form - Angular
This tutorial shows you how to create a template-driven form. The control elements in the form are bound to data properties that have...
Read more >
Validation message not restricting form from submitting
When user is on interface corresponding to link 1 i.e. he clicks on link Form 1 and he clicks on Submit button without...
Read more >
Handling Validation Errors in Forms - Up Your A11y
Approach 1: Provide a clear list of errors when 'submit' is clicked · Perform validation on all inputs when the button is clicked...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found