Is it possible to insert jQuery Validate in PHP?

Asked

Viewed 314 times

2

For example, in my php code is like this:

if( @$_SERVER['REQUEST_METHOD'] == 'POST' ) {
  $sobrenome =      $_POST['sobrenome'];
  $erro = '';

  if( $nome == 'Qual é o seu sobrenome' ) {
     $erro .= '';
  } elseif ( strlen( $sobrenome ) < 2 ) {
     $erro .= 'Insira um sobrenome existente<br>';
  }
?>

The result is this, the interface is not pleasant inserir a descrição da imagem aqui

I would like that through jquery Validate would look like a label error, a more pleasant interface. inserir a descrição da imagem aqui

  • By all indications, its validation is only being done by PHP, there is, or did not inform the use of any validation plugin. Anyway, you can validate by PHP and make use of a Tooltip to create the 'balloon' on the field that contains error.

  • That’s what I needed @Papacharlie

  • validation is by the server, NEVER omit PHP validation, because if the user disables js, you will not have any validation.

  • @Papa Charlie Great websites for example Facebook, Gmail uses this method

  • I never hear any complaints from users

  • Have you ever worked on facebook or gmail? But if you know well about security, ok good luck.

  • @Papacharlie I am aware that php is the best method to validate fields, but it validates with a Beta interface, jquery validates with a more modern interface

  • 2

    You certainly have no idea what you’re talking about, but that’s fine. I won’t lengthen the comments here, but if you want to use the chat can call me.

  • 1

    @Papacharlie Thank you very much I understood well is a very simple way, it was what I was exactly looking for

  • 2

    Cool. It was a basic model, just increase as your need.

Show 6 more comments

2 answers

2


I have a code that I created in JS that uses together with jQuery Validator, where it validates all form fields that have the attribute required, this way you don’t need to be validating one by one.

The only drawback is that it has no specific error message for each field and only validates whether the field has been filled or not, but I believe I can already help.

As @Papacharlie said, it’s a more aesthetic validation.

$('form').not('.no-validate').each(function(key, form) {
    $(form).data('validator', $(form).validate({
        ignore : "",
        errorElement : false,
        highlight : function(field) {
            $(field).closest('.form-group, .input-group').addClass('has-error').removeClass('has-success');
        },
        unhighlight : function(field) {
            $(field).closest('.form-group, .input-group').removeClass("has-error").addClass('has-success');
        },
        errorPlacement : function() {
        }
    }));
 });

For this code to work just you have the input within a div with class form-group or input-group, and have your CSS class has-error and has-success created, see below how I created the classes.

.has-error .form-control, .has-error input[type="email"]{
    border-color: #A94442;
    -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
    box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
}

.has-success .form-control {
  border-color: #3c763d;
  -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
  box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
}

These two classes are present in Bootstrap if you use.

In this code you can improve by adding a div with the error message you want, or hide the div if the field is filled in, etc.

It’s just an idea and a quick and simple way to validate whether the required fields have been filled in.

-1

In this case, I recommend to treat these validations before arriving in PHP with own plugin you suggested, because if you leave to validate in PHP, you will have to write HTML code in PHP code to try to improve the look, that would be very unpleasant, or would have to cast the exceptions for jQuery to treat again.

  • my form has captcha these plugins valida captcha also?

  • 1

    And if the user disables javascript?

  • 1

    Yes it is possible to use pickup. @Papa Charlie has to validate on both sides, I always say that client-side validations are only aesthetic for a more immediate and friendly result, validating the server-side is paramount. You could also not allow access if Javascript was disabled and if there were interactions very dependent on it, this is what happens to other websites like email, but still validations on the server must exist.

  • @vcrzy Show me a cool site to download this plugin

  • 1

    Look here friend, https://github.com/jzaefferer/jquery-validation even teaches how to use it. Aprs

Browser other questions tagged

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