Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

HTML5 Form Custom Validation Message Using setcustomvalidity()

Posted on 2014-01-23
5
Medium Priority
?
8,294 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 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

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

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

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
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 the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

571 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