Javascript - How to validate fields in a form

3
  

I am creating a form that I need to validate: Email, CPF, Birth Date, Contact Phone, Optional Phone .

Someone knows how to do this with that "mask", that when the person types the date for example, does not need to insert the "/" slashes, javascript does this. Just like the other fields I've listed?

Another thing, in the other fields like Name, Address, etc ... , if they are not entered, sends an alert asking to be inserted.

Example:

Note: Pure Javascript .....

    
asked by anonymous 21.08.2016 / 16:54

2 answers

4

You can use an external library called Lightweight Pure JavaScript the name says it all.

Basic Usage

Import the file

<script src="input-mask.js" ></script>

Application example for the Data field

new InputMask().Initialize(document.querySelectorAll("#date"),{
  mask: InputMaskDefaultMask.Date, 
  placeHolder: "Date: 01/01/2015" 
});

In the library you will find more examples like phone validation and Social Security Number (SSN) (like the identity here in Brazil) you can read the examples and apply to your page the way you want.

You can view this external library by running here .

Click here to view the Source.

    
28.08.2016 / 01:41
11

You can use the jQuery Mask to perform the input formatting: The code looks like this:

 $('.cpf').mask('000.000.000-00', {reverse: true});
 $('.date').mask('00/00/0000');
 $('.phone_with_ddd').mask('(00) 0000-00000');

And for validation you can either use HTML5 validation or use a plugin for this, such as Parsley .

When you have more formed and not-so-general questions like this, open a new specific question.

    
21.08.2016 / 18:17