Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
Will jQuery or AJAX make HTML "required" attribute invalid?

I'm now constructing a registration page and have labeled username and password inputs as required. Simple code as:

Username <input id="username" type="text" required/>
Password <input id="username" type="password" required/>
<button id="register">Register</button>
<div id="registerOutput></div>

When I just run those code with pure HTML on localhost the "required" would function properly. But once I include a register.js file which I made to get the data and use AJAX to send data to register.php, the required attribute seems not working. In register.js, simple code as:

$(document).ready(function(){
    $("#register").on("click", function(){

        var username        = $("#username").val();
        var password        = $("#password").val();

        var userData = "username=" + username + "&password=" + password;

        $.ajax({
            method: "post",
            url: "register.php?",
            data: userData,
            success: function(backData){
                $("#registerOutput").html(backData);
            }
        });
    });
});

Can anybody tell me the reason why jQuery would override the functionality of required? How to surpass it or if it must override, how should I do the same thing in my jQuery code?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You might want to redesign your code a little bit. There is a way to follow HTML convenience and yet use full power of AJAX.

I edited your example in jsfiddle: https://jsfiddle.net/myezf63b/

So here is modified HTML:

<form id="form">
  Username <input id="username" type="text" required/>
  Password <input id="username" type="password" required/>
  <button type="submit" id="register">Register</button>
</form>

<div id="registerOutput">
</div>

Basically I added type="submit" to button and wrapped it inside form.

and this is modified jQuery code:

I added e.preventDefault() which prevents website from redirecting (reloading page) and as event I used submit which follows this pattern.

$(document).ready(function(){ $("#form").on("submit", function(e){
    e.preventDefault()
    var username        = $("#username").val();
    var password        = $("#password").val();

    var userData = "username=" + username + "&password=" + password;
        console.log('test')
    $.ajax({
        method: "post",
        url: "register.php?",
        data: userData,
        success: function(backData){
            $("#registerOutput").html(backData);
        }
    });
  });

});
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!