Validation of <input type="text" required name="name" ng-model="warning.name" />

Asked

Viewed 127 times

0

Honestly, it’s been 3 days that I’m trying to validate an input, the business theoretically seems simple, but in practice is not validating anything.

The situation is as follows:

This is a part of my index.html

<script>
    function checkForm(form) {
        // validation fails if the input is blank
        if (form.name.value == null || form.name.value == '' || form.name.value == "") {
            alert("Existe Algum Campo Vazio (Isso Não Pode Acontecer)");
            form.name.focus();
            return false;
        }
        return true;
    } </script>

Here comes my head followed by form

<form onsubmit="return checkForm(this);">
        <div>
        Name: <input type="text" ng-model="aviso.nome" required name="name"
                value="" aria-describedby="name-format" />  
        </div>
        <input type="submit" ng-click="SalvarAvisos()" /> 
        <input type="button" value="limparCampo"    ng-click="limparCampo()" />
</form>

I lay all, looking like this seems to me that there has to be some validation in the

ng-click="Save warnings()"

but I am lost, because even though the field is empty or null, the error message appears, but the form is sent in the same way, I know that it is sent pq in the database appears there "null".

Obg for getting to the end. Bomdia&boaspesquisas

  • If you are using Angularjs why not validate the elements by your models applied through $context?

  • I’ll search this $context , there’s a lot of front stuff, I’m picking up this full pace yet.

1 answer

0


Anderson, there must be a better way to validate this, since you are using Angular. However, here are some editions that can satisfy you:

  1. take out the event onsubmit of <form> and add a name to him (name="formularioXYZ")
  2. in the method checaForm(), place document.formularioXYZ.submit() before the return true
  3. change the type="submit" for type="button"

If you don’t want to submit, just execute SalvarAvisos(), do so:

  1. take out the event onsubmit of <form>;
  2. in the method checaForm(), place SalvarAvisos() before the return true;
  3. change the type="submit" for type="button";

Hugs and good luck with your project!

Browser other questions tagged

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