Problems with HTML function

Asked

Viewed 52 times

-3

Good afternoon guys all right ?

I’m trying to call a function I made to validate a null field of html, but the function is not calling.

I have a file name: Verificacampos.js

function Validacao(f) {
    if (f.razao_social.value.trim == "") {
        alert("Por favor empreencha o campo");
        f.razao_social.focus();
        return false; 

        }
    }

html that does not call the function on onsubmit:

<head>
<title>Cadastro Prestador</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<!-- Bootstrap -->
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<script language"javascript" type="text/javascript" src="Verificacampos.js"></script>
</head>



<body>
<!-- menu -->

<nav class="navbar navbar-inverse">
  <div class="container-inverse">
        <div class="navbar-header">
            <a class="navbar-brand" href="#">Cadastro de Prestador</a>
        </div>

  </div>
 </nav>

 <style>


        #cont1{
        border: solid;
        margin-top: 2%;
        padding: 1%;
        padding-bottom: 2%;
    }

    #btn_cad{
        float: right;
    }


 </style>

<div class="container">
<div class="panel panel-primary">
      <div class="panel-heading">Prestador</div>
      <div class="panel-body">
        <form name= "Ficha1" action="cadastroprestador.php" onsubmit="return Validacao()" method="post">
            <div class="form-group" id="Prestador">
                <div class="col-lg-2">
                <label for="ex1">ID </label>
                <input class="form-control" name="id" type="text" >
            </div>
            <div class="col-lg-12 form-group">
                <label for="ex2">Razão Social</label>
                    <input class="form-control" name="razao_social" type="text">
            </div>
            <div class="col-lg-6 form-group">
                <label for="ex3">Nome Fantasia</label>
                    <input class="form-control" name="nome_fantasia" type="text">
            </div>
            <div class="col-lg-6 form-group">
                <label for="ex4">CNPJ</label>
                    <input class="form-control" name="CNPJ" type="text">
            </div>      
            <div class="col-lg-6 form-group">
                <label for="ex6">Tipo</label>
                    <select class="form-control" name="tipo">
                            <option>HOSPITAL</option>
                            <option>CLINICA</option>
                            <option>LABORATORIO</option>
                            <option>REMOÇÃO</option>
                            </select>
            </div>  
            <div class="col-lg-6 form-group">
                <label for="ex5">Indicação</label>
                    <select class="form-control" name="indicacao">
                        <option>Prospecção</option>
                        <option></option>
                        <option></option>
                        <option></option>
                        </select>
            </div>



      </div>
    </div>
</div>
<div class="panel panel-primary">
      <div class="panel-heading">Logadouro</div>
      <div class="panel-body">

            <div class="col-lg-12 form-group">
                <label for="ex2">Endereço</label>
                    <input class="form-control" name="endereco" type="text">
            </div>
            <div class="col-lg-6 form-group">
                <label for="ex3">Numero</label>
                    <input class="form-control" name="Numero" type="text">
            </div>
            <div class="col-lg-6 form-group">
                <label for="ex4">Bairro</label>
                    <input class="form-control" name="bairro" type="text">
            </div>      
            <div class="col-lg-6 form-group">
                <label for="ex6">Cidade</label>
                    <input class="form-control" name="cidade" type="text">
            </div>
            <div class="col-lg-6 form-group">
                <label for="ex6">UF</label>
                    <input class="form-control" name="UF" type="text">
            </div>
            <button type="submit" class="btn btn-primary col-lg-2 right"  id="btn_cad" >Cadastrar</button>      
        </form> 
      </div>
    </div>
</body>
</html>
  • It would be better to use the 'required' attribute in the input instead of using javascript.

2 answers

1

Two errors:

Missed the this in the:

 onsubmit="return Validacao(this)"
                             ↑↑

And the parentheses in the method .trim():

if (f.razao_social.value.trim() == "") {
                             ↑↑
  • worked out sam obg

  • 1

    When an answer solves a question, mark it with , including the one you did here

  • If you don’t know how to dial, give a read on this page

0

In the html file inform to the function the name of the FORM:

<form name= "Ficha1" action="cadastroprestador.php" onsubmit="return Validacao(Ficha1)" method="post">

In the file Verificacampos.js change the code snippet below

if (f.razao_social.value.trim == "") {

for:

if (f.razao_social.value.trim() == "") {

Browser other questions tagged

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