duanlan3598 2015-09-15 01:56
浏览 37
已采纳

在空白文本字段中显示消息错误

I have a problem showing an error message if the text field is blank. I want a simple message, eg "can't be empty". Once user click button submit and there are several blank text fields then system should be show error message "can't be empty" below the text field and the text field also should be turn red color.

What happen now is, when I click button submit then the system only focus on the blank text field but no error message and the text field is blue color.

this my form:

<form id="myForm2" action="addnewreservation.php" method="post">
  <div class="panel panel-default is-header">
      <div class="panel-body">

        <table class="table table-bordered">
          <tbody>
            <tr>
                <td>Title </br>
                <input class="form-control" name="title" type="text"></br>
                <button class="btn btn-edit btn-sm pull-right" id="reserveroom" type="submit" name="submit">Book Meeting Room Now!</button>
                </td>
            </tr>
          </tbody>
        </table>

      </div>

  </div>
</form>

this is script:

$(document).ready(function(){
 $('#myForm2').formValidation({
    message: 'This value is not valid',
        icon:{
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields:{
            title: {
                validators: {
                    notEmpty: {
                        message: 'can\'t be empty'
                    }
                }
            }

        }
   })
});



 $('#myForm2').change(function(e) {
    $('reserveroom').focus();
});
  • 写回答

2条回答 默认 最新

  • doupang5433 2015-09-15 02:10
    关注

    You should use jQuery validation plugin. It should do the trick.

    http://jqueryvalidation.org/

    Some code example:

    <form id="myForm2" action="addnewreservation.php" method="post">
        <div class="panel panel-default is-header">
          <div class="panel-body">
    
            <table class="table table-bordered">
              <tbody>
                <tr>
                 <td>Title </br>
                  <input class="form-control" name="title" type="text" required></br>
                  <button class="btn btn-edit btn-sm pull-right" id="reserveroom" type="submit" name="submit">Book Meeting Room Now!</button>
                 </td>
                </tr>
              </tbody>
            </table>
    
          </div>
    
        </div>
    </form>
    

    And then just

    $(document).ready(function(){
       $('#myForm2').Validate();
    });
    

    EDIT:

    Of course dont forget to include plugin file right after calling jQuery script

    <script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.14.0/jquery.validate.min.js"></script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私