Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 3: Forms > Overriding browser defaults

Overriding browser defaults

Built-in validation messages are great, but what if you want to customise these error messages? What if it’s Talk Like a Pirate Day? Perhaps I want to change all the validation messages to speak like an angry pirate, too.

It’s possible, with a bit of JavaScript via setCustomValidity. However, by setting a custom message, it causes the field to be invalid in the first place, so the workaround is to first set the custom validity to an empty string—clearing any custom error—perform a validity check manually in the code, and then set the custom message so that it’s then presented to the user.

So instead of reading:

humptydumpty is not a legal email address

We’ll change the validation to read the following in “traditional” pirate speak:


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint