Solved

HTML5 Form Custom Validation Message Using setcustomvalidity()

Posted on 2014-01-23
5
5,238 Views
Last Modified: 2014-02-09
Hi.

Newbie to HTML5 forms.

Trying to change the validation messages.

I have been reading all over the net that you can change the messages by using a "setcustomvalidity()" function, or something, but... I can't figure out how to use it.


1. Do I have to apply the "setcustomvalidity()" function to every "required" form field?

2. What is a simple, working example of maybe 2 or 3 fields of how to implement this?



Thank you!
Shane
0
Comment
Question by:lshane
  • 3
  • 2
5 Comments
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 500 total points
ID: 39804986
The method/function is part of the DOM API which may not be fully implemented in all browsers and is of course not support at al in old browsers.

Setting the text of the message is a simple one line per input bit of javacript that need to be run after the page is loaded:

document.getElementById('password1').setCustomValidity('Passwords must be at least 16 characters in length');

In this case the input must have an id of 'password1'.

There is a nicely detailed description of the whole validation constraint api here:
http://www.html5rocks.com/en/tutorials/forms/constraintvalidation/

Cd&
0
 

Author Comment

by:lshane
ID: 39805215
Thanks for the reply, COBOLdinosaur.

So, I may need a little hand-holding...

I had already read that page during my internet scour, but could not tie the loose ends... they keep changing examples down the page, so I couldn't follow.


Here is a very simple, 2-field form.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<form action="" method="post" name="form1">

  <p>
    Name: 
    <input name="fmname" type="text" id="fmname" required />
    
  </p>
  <p>
E-mail: 
<input type="email" name="fmemail" id="fmemail" required />
  </p>
  <p>
    <input type="submit" name="fmsubmit" id="fmsubmit" value="Submit" />
  </p>
</form>

</body>
</html>

Open in new window



1. What do I need to put in the <HEAD> of my page, if anything?

2. Could you, please, show me how to apply the "setcustomvalidity" to my INPUT fields?


I feel if I could see a "full" example of how to apply this, then I can adapt and move from there, but I feel like all the examples online are just "pieces" of the whole picture.

Thank you,
Shane
0
 

Author Comment

by:lshane
ID: 39808111
Ok, so I'm guessing my last post/request was a little too "open-ended".



I found a basic example that I will paste here.

Two questions, though:
1. The form validates with a type mismatch, HOWEVER, I am wanting it to validate as a required field, so that if nothing is entered, and the form is submitted, it will have a custom message.  I have tried replacing the "email.validity.typemismatch" with "emai.validity.valuemissing", but that does not seem to work, so I guess I'm doing something incorrectly.  **So, I guess on the E-mail, I am wanting it to validate for BOTH the e-mail and the "required".  

2. How do I efficiently add "required" validation for the second field of "fmfirst"?

Here is the form:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>TESTING FORM</title>

	
</head>
<body>
 
<form>
  <label for="mail">Enter E-mail:</label>
  <br />
  <input type="email" id="mail" name="mail" required>
  <br />
  <br />  
  <label for="fmfirst">Enter your first name:</label>
  <br />
  <input type="text" id="fmfirst" name="fmfirst" required>
  <br />
  <button>Submit</button>
</form>


<script>

var email = document.getElementById("mail");

email.addEventListener("keyup", function (event) {
  if(email.validity.typeMismatch) {
    email.setCustomValidity("Need and E-mail, please!");
  } else {
    email.setCustomValidity("");
  }
});

</script>
 
</body>
</html>

Open in new window



Please help.

Thanks,
Shane
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 39809141
Okay I think the only problem you have is a matter of timing.  If you are doing the script in the head of the page then you are referencing to the document before it is fully loaded.

The solution is to put it in a function and invoke the function with a load event; or put the scripting as the last thing in the page.  I generally prefer a load event to avoid maintenance issues.

Cd&
0
 

Author Closing Comment

by:lshane
ID: 39846202
Ok - well, I'm still not sure how to actually implement it, but I suppose this answer gets me started.

Thanks.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now