Javascript的表單驗證長度,javascript表單

來源:互聯網
上載者:User

Javascript的表單驗證長度,javascript表單

JavaScript 可用來在資料被送往伺服器前對 HTML 表單中的這些輸入資料進行驗證。

該採用什麼樣的方式對使用者作出提醒呢?你一定不想用alert()提示框

在輸入欄位後加一個sqan標籤

<input id="phone" name="phone" type="text" size="12" onBlur="validateNonEmptyFun(this,document.getElementById('phone_help'));" /><span id="phone_help" class="help"></span>function validate_Length(inputFiled,helpText){//如果輸入欄位內容是空,則在span標籤內提醒if(inputFiled.value.length==0){if(helpText!=null)helpText.innerHTML="文字框不可為空";}//如果輸入欄位不空,則清空span標籤內的內容else if(helpText!=null)helpText.innerHTML=""}</script>

helpText是傳入的span對象

用span標籤來為使用者作出提醒,不會像alert那樣阻擋使用者視覺

除了非空驗證,還有尺寸問題

驗證資料長度

<input id="phone" name="phone" type="text" size="12" onBlur="validate_Length(1,32,this,document.getElementById('phone_help'));" /><span id="phone_help" class="help"></span>

這裡的參數變成了四個,第一個是文本最小長度,第二個是文本最長長度

function validate_Length(minLegth,maxlength,inputFiled,helpText){if(inputFiled.value.length<minLegth||inputFiled.value.length>maxlength){if(helpText!=null){helpText.innerHTML="請輸入長度為"+minLenght+"到"+maxLength+"的文本";return false;}}else if(helpText!=null){helpText.innerHTML=""return true;}}

驗證郵遞區號

function validate_ZipCode(inputFiled,helpText){if(inputFiled.value.length!=5){if(helpText!=null)helpText.innerHTML="郵遞區號長度必須為5位";return false;}else if(isNaN(inputFiled.value)){if(helpText!=null)helpText.innerHTML="郵遞區號必須為數字";return false;}else if(helpText!=null){helpText.innerHTML=""return true;}}

您可能感興趣的文章:
  • JavaScript 表單驗證Regex大全[推薦]
  • 實用的JS表單驗證提示效果
  • js 常用Regex表單驗證代碼
  • jquery validate.js表單驗證的基本用法入門
  • 簡單的js表單驗證函式
  • js用Regex來驗證表單(比較齊全的資源)
  • javascript表單驗證使用樣本(javascript驗證郵箱)
  • Extjs表單常見驗證小結
  • jquery表單驗證外掛程式(jquery.validate.js)的3種使用方式
  • javascript製作的簡單註冊模組表單驗證

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.