Javascript表单验证无法在bootstrap模式

时间:2017-05-26 16:23:15

标签: javascript html twitter-bootstrap

我正在制作带有表单的bootstrap模式,并且我有自定义JavaScript字段验证并且它不起作用,我在没有模态的普通页面中尝试了相同的操作并且它完美地工作。

以下是JavaScript代码:

function validartitulo()
{
    var titulo = form.titulo;
    if (titulo.value == "") {
    document.getElementById("msgtit").innerHTML = "<font color='red'>Este campo é obrigatório.</font>";
    return false;
}
if(titulo.value.length <= 25){
    document.getElementById("msgtit").innerHTML = "";
    return true;
    }
        else if(titulo.value.length>25){
        document.getElementById("msgtit").innerHTML = "<font color='red'>O tamanho máximo é 25!</font>"; 
            return false;}
        }

       function validar(){
    if(validartitulo() && validarlink())
        return true;
    return false;
}

和表格:

 <form id="luForm" class="form-horizontal form-label-left input_mask"    name="form" enctype="multipart/form-data" method="POST"  action="videos/alt_vid2.php" onsubmit="return validar()">

<div class="form-group">
   <label class="col-md-4 control-label"><font color="black">Titulo: </font></label>  
   <div class="col-md-4 inputGroupContainer">
   <div class="input-group">
   <span class="input-group-addon"><i class="glyphicon glyphicon-tag"></i> </span>
  <input  style='text-align:center;' name="titulo" value="<?php echo $lin['titulo']; ?>" class="form-control"  type="text" onblur="return validartitulo()">
    </div>
  <div id="msgtit"></div>
  </div>
</div>

控制台错误:

Uncaught TypeError: Cannot read property 'value' of undefined
at validartitulo (eval at globalEval (jquery.min.js:2), <anonymous>:3:12)
at HTMLInputElement.onblur

2 个答案:

答案 0 :(得分:0)

尝试改变这一点:

function validartitulo()

将其更改为:

function validartitulo(form)

并且

<input  style='text-align:center;' name="titulo" value="<?php echo $lin['titulo']; ?>" class="form-control"  type="text" onblur="return validartitulo()">

将其更改为:

 <input  style='text-align:center;' name="titulo" value="<?php echo $lin['titulo']; ?>" class="form-control"  type="text" onblur="return validartitulo(this.form)">

答案 1 :(得分:0)

解决了它,这是一个未被捕获的错误,所以有人建议我将<script>(如果它在head中移动到body,感谢所有人!