1. 程式人生 > >jQuery驗證文字框內容不為空

jQuery驗證文字框內容不為空

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

                       

通過$.fn 擴充套件jQuery方法

/** * 校驗文字是否為空 * tips:提示資訊 * 使用方法:$("#id").validate("提示文字"); * @itmyhome */
$.fn.validate = function(tips){    if($(this).val() == "" || $.trim($(this).val()).length == 0){        alert(tips + "不能為空!");        throw SyntaxError(); //如果驗證不通過,則不執行後面    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

使用

<form action="">    姓名: <input type="text" id="name" name="name" />    年齡:<input type="text" id="age" name="age" />    地址:<input type="text" id="address" name="address"
/>
    <input type="button" value="提交" onclick="submit();" /></form>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
function submit(){    //呼叫validate()    $("#name").validate("姓名");    $("#age").validate("年齡");    $("#address").validate("地址");}
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

作者:itmyhome

           

給我老師的人工智慧教程打call!http://blog.csdn.net/jiangjunshow

這裡寫圖片描述