Javascript - How to validate form fields

Asked

Viewed 933 times

3

I’m creating a form that I need to validate: E-mail, CPF, Birth Date, Contact Phone, Optional Phone.

Someone knows how to do this with that "mask", that when the person type the date for example, does not need to enter the "/" bars, javascript does this. Just like in the other camps I mentioned?

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

Example:

inserir a descrição da imagem aqui

Remark: Pure Javascript.....

  • You’re asking too much

2 answers

11

You can use the jQuery Mask to perform input formatting: The code will look 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 use HTML5 validation or use a plugin for this, such as Parsley.

When you have more formed questions and not so generic, open a new specific question.

  • Amigo, I’d like to do it using pure javascript. It’s possible?

  • 2

    I recommend that you use a plugin for this, I know that we have as a premise do everything to ensure stability. But keep in mind that using a plugin you will already have solved problems that other people in the community have gone through and contributed to a project so that others do not pass. In the case of masks, there are many compatibility problems mainly in mobile.

  • I’m gonna run some tests. :)

  • Friend, and that Parsley, what the code would look like?

  • The implementation of Parsley is very complex, I recommend you read its documentation and take a look at the example: http://parsleyjs.org/doc/examples/simple.html#

  • Worse that I’m not very good with JS, much less with jQuery. This seems to be a not very viable alternative for me.

  • You could use HTML5 validation by putting the required However, you won’t be able to leave the validation correct, but in the case of required it will work in new browsers except IE8. What can generate spam with incomplete forms.

  • No friend, it’s not a contact form.

  • Friend to use this plugin jQuery Mask, I pull it from where?

  • You can use a dependency manager or download it from github: https://raw.githubusercontent.com/igorescobar/jQuery-Mask-Plugin/master/dist/jquery.mask.min.js

  • +100 for you friend!

Show 6 more comments

5


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

Uso Básica

Import the file

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

Application example for the Date field

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

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

You can view this working external library here.

Clicking here to view the Source.

  • Still not the answer I hope. :(

  • 1

    @Alexandrelopes what I can do to improve my answer ?

  • It’s all too complex. It was supposed to be so simple :'(

  • 3

    Alexandre.. Simpler than you have been told would be someone doing their job..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.