Solved

HTML5 Form Custom Validation Message Using setcustomvalidity()

Posted on 2014-01-23
5
6,585 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 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

726 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