Solved

HTML5 Form Custom Validation Message Using setcustomvalidity()

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Jquery form 9 40
arrays and buttons with user input 2 34
Html split(text) 2 28
Looking for solution to pass data to 3rd-party web platform? 4 28
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: blockā€¦
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

749 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