Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

HTML5 Form Custom Validation Message Using setcustomvalidity()

Posted on 2014-01-23
5
Medium Priority
?
7,514 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 2000 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will learn how to dynamically set the form action using jQuery.

610 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