Click here to Skip to main content
15,559,819 members
Articles / Web Development / HTML5
Posted 23 Oct 2016


8 bookmarked

JavaScript jrValidator to Validate HTML Forms

Rate me:
Please Sign up or sign in to vote.
4.94/5 (9 votes)
29 Oct 2016CPOL3 min read
jrValidator provides a set of JavaScript functions to validate HTML forms


HTML5 has provided a great feature to validate forms but still it's not very helpful while handling it in JavaScript, and it has always been annoying to write separate codes for each form each time. Here is the JavaScript jrValidator to get rid of this problem. The set of JavaScript functions in jrValidator is light-weight, simple and very easy to use and modify the code. It has been written to meet the requirements of basic form validation.


How to Add this Source to a Project?

  • Download jrValidator src file from github
  • Add jrValidator.min.css and jrValidator.min.js to your script where the form should be validated from the src folder:
  1. HTML
    <!--add jrValidator.min.css at the top of your script-->
    <link href="src/jrValidator.min.css" rel="stylesheet">
  2. HTML
    <!--add jrValidator.min.js at the end of your script-->
    <script href="src/jrValidator.min.js"></script>
  • Now validate your form using the guidelines given below:

NOTE: Don't forget to add jquery library.

Validating a Form using jrValidator

There is a set of HTML attributes that should be included in form tags to validate it. We will discuss these attributes later in this article.

Using jrValidateForm() Function

This function validates form and returns true or false:

/*here 'form_id' is the id of form to be validated and variable formValidated will store the info 
whether the form is validated or not*/

var formValidated = jrValidateForm('form_id');



<form id="form_id">
  <input type="button" onclick="submitForm('form_id');">


function submitForm(form_id){
    //code to handle if the form is validated
    //code to handle if the form is not validated

Set the input as Compulsory Field

Use attribute required in the compulsory inputs:


<input type="email" required>

<!--also applicable on select-->
<select type="gender" required>

Using custom error message for Each Invalid Input

Use attribute data-validation-message in the inputs you want to set custom error messages.


<input type="email" data-validation-message="This is my custom error message">

If you are happy with the default error messages, then don't add this attribute to the input:

Set accepted file formats for File Type Inputs

Use attribute data-accepted-file-format in the file type inputs:


<input type="file" data-accepted-file-format="jpg, gif, png" 
data-validation-message="This is my custom error message">

There is a default message box to show the error message (as given in the image below), but you can also get the message at your preferred location:

Image 1

Common error message box for all the Inputs

Use attribute data-message-loc in the form tag:


<form type="email" data-message-loc="msg-box-id">

Note: Here 'msg-box-id' is the id of common error message box.

Image 2

Exclusive error message box for Each Input

Use attribute data-message-loc in each input tag:


<input data-message-loc="msg-box-id1">
<input data-message-loc="msg-box-id2">

Image 3

Using data-custom-regex Attribute to Define the Custom Format for Inputs

You can simply add data-custom-regex attribute to the input to set the format of input value according to your needs.


For example, if you want to take a 10 digit number from the user, you can simply write this code:

<input type="text" data-custom-regex="^[0-9]{10}$">

NOTE: Here, "^[0-9]{10}$" is the regex for a 10 digit number.

Using data-password-pattern Attribute to Validate Passwords

There are 5 types of password patterns in jrValidator, these are discussed below:

Type 1: Minimum 8 characters, at least 1 Alphabet and 1 Number:
Example: valid12345
<input type="password" data-password-pattern="1">
Type 2: Minimum 8 characters, at least 1 Alphabet, 1 Number and 1 Special Character:
Example: valid#12345
<input type="password" data-password-pattern="2">
Type 3 : Minimum 8 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet and 1 Number:
Example: Valid12345
<input type="password" data-password-pattern="3">
Type 4 : Minimum 8 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet, 1 Number and 1 Special Character:
Example: Valid@12345
<input type="password" data-password-pattern="4">
Type 5: Minimum 8 and Maximum 10 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet, 1 Number and 1 Special Character:
Example: Valid@123
<input type="password" data-password-pattern="5">
Custom: Provide your own regex to validate password
Example: 1234587100
<input type="password" data-custom-regex="^[0-9]{10}$">

Validating Retype-Passwords

It can be done by simply adding two attributes data-password-pattern and data-for-password-id in the password type input:

<input type="password" name="re_password" data-password-pattern="retype" 

NOTE: Here, "user_password" is the id of password type input which is to be matched with the retype password.

Glance Over the Attributes Used

  • required
    • used to specify that the input field must be filled by the user
  • data-validation-message
    • Specify the custom error message
  • data-accepted-file-format
    • Specify the file format to be accepted by file type input
  • data-message-loc
    • Specify the location where error message will be displayed
  • data-custom-regex
    • Specify the custom format of input (accept regex only)
  • data-password-pattern
    • Specify the custom format of input (accepts the only regex)
  • data-for-password-id
    • Specify the id of password to be matched with re-password


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
India India
I am a java lover, with some experience of working in web development and mobile development. I have a keen interest in developing java libraries and utility classes. Am not so experienced in software development but still i love to share whatever i know and use techniques. I enjoy learning new things, new stuffs to be a better developer.

Comments and Discussions

QuestionNice article Pin
Rahul VB30-Oct-16 6:26
professionalRahul VB30-Oct-16 6:26 
Questionintegration in frameworks would be great Pin
alpham827-Oct-16 9:31
alpham827-Oct-16 9:31 
Questionimage Pin
Nelek23-Oct-16 12:59
protectorNelek23-Oct-16 12:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.