All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

jQuery Required Field Validation On Submit

Last Updated : Mar 11, 2024

jQuery Required Field Validation On Submit

In this article we will show you the solution of jQuery required field validation on submit, here we needs to create form with input field then we can add required field by using attr() method after submission of submit button.

As we know attr() method used for sets any attributes value on any html elements so we can add required on input field when submit button submitted by users.

Step By Step Guide On jQuery Required Field Validation On Submit :-

Here we defined html block with form and div element for showing defined error on webpage then within form we defined two input elements, first input element text type and second input element is submit type for generate submit button.

In script block we defined ready() method which is automatically starts loading code of within this function when this program opened on browser and within that we waiting for click event occurrence which is appended with ‘submit’ button by specifying id ‘sub’ then we sets required attribute’s value as ‘true’ on input element for validate empty fields.

<!DOCTYPE html>
<html>
    <head>
        <title>Add Required Attribute On Submit</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script>
            $(document).ready(function (){
                        $('#sub').click(function(){
                            $('input').attr('required',true);
                            $('#err').text("Input Needed");
                        })
            })
        </script>
        </head>
        <body>
            <form>
                <input type="text" id="inp">
                <input type="submit" id="sub" value="Submit">
            </form>
            <div id="err"></div>
        </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Here we imported open source jquery library support file which is needed for coding using jquery in program.
  5. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  6. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  7. In script we defined ready() method within that we appending click() method with button ‘submit’ by using id ‘sub’ for generate click event when user clicks on ‘Submit’ button.
  8. Within function we sets ‘required’ attribute value to ‘true’ for adds required attribute on input element and also we inserting ‘Input Needed’ error message on div element by specifying id ‘err’ with text() method.
  9. In html block we defined form within form we defined two input elements with different type’s first one is text type which for collect input from user and another is submit type for submits user response.
  10. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to set required field on submit in jquery.

Before execution of program we needs to confirm internet connection because then only that jquery file will supports defined jquery codes without error.

When we executes program on browser we can see input field and submit button now user needs to click on submit button before filling input fields then required attribute added on input we can see in the form of default error and defined error and for clarification we can see on console panel’s Element tab.

I hope this article on jQuery required field validation on submit helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪