I am getting a really weird behaviour where I noticed that jquery only executes if there is a dummy javascript prior to it.
With javascript (with alerts command) coded before the jquery scripts
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0" xmlns:c="http://java.sun.com/jsp/jstl/core"> <jsp:directive.page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" session="true" language="java" /> <jsp:output doctype-root-element="html" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" omit-xml-declaration="true" /> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <link rel="stylesheet" type="text/css" href="/css/goeasyhome_logo.css" /> <link rel="stylesheet" type="text/css" href="/css/login.css" /> <script src="/javascript/jquery-2.2.1.min.js"></script> <script type="text/javascript"> alert('x'); </script> <script type="text/javascript"> $(document).ready(function() { $('input[name="j_username"]').focus(); $('#email input').on("invalid", function(event) { if (!$(this).val()) { this.setCustomValidity("Please fill in this field 2"); } else { this.setCustomValidity("Please enter a valid email address 2"); } }); $('#email input').on("change", function(event) { if ($(this).val()) { this.setCustomValidity(""); } }); }); </script> </head> <body> <br /> <br /> <br /> <header> <div id="header-center-11x12"> <img id="goeasyhome_logo_12x12" src="/images/goeasyhome_basic.png" alt="goeasyhome" /> </div> </header> <br /> <div id="main-content"> <form method="post" action="j_security_check"> <div id="email"> <input type="email" placeholder="Email Address" name="j_username" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}" required="required" /> </div> <div id="password"> <input type="password" placeholder="Password" name="j_password" required="required" /> </div> <br /> <div id="signin"> <input type="submit" value="Sign In" /> </div> </form> <br /> <a class="passwdsignup-color" href="/forgotpassword.jsp">Forgot Password? </a> <a class="passwdsignup-color signup-position" href="/signup.jsp">Sign Up?</a> </div>
When i submit without any data e.g. no email address, the correct validation message pops up as shown below.
With javascripts (alerts command) commented out.
<head> <link rel="stylesheet" type="text/css" href="/css/goeasyhome_logo.css" /> <link rel="stylesheet" type="text/css" href="/css/login.css" /> <script src="/javascript/jquery-2.2.1.min.js"></script> <!-- <script type="text/javascript"> alert('x'); </script> --> <script type="text/javascript"> $(document).ready(function() { $('input[name="j_username"]').focus(); $('#email input').on("invalid", function(event) { if (!$(this).val()) { this.setCustomValidity("Please fill in this field 2"); } else { this.setCustomValidity("Please enter a valid email address 2"); } }); : :
You'll noticed the default message 'Please fill in this field' instead of the 'Please fill in this field 2' is displayed. The jquery command didn't run.
This is bizzare to me.
Any help would be great.
Here's the jsfiddle https://jsfiddle.net/yapkm01/3f321g84/