»
Tiếng AnhTiếng PhápTiếng Việt

In - Giới hạn khung nhập liệu với RegEx - JavaScriptBank.com

Phiên bản đầy đủ: jsB@nk » Biểu mẫu » Giới hạn khung nhập liệu với RegEx
URL: https://www.javascriptbank.com/javascript-regex-example-code-text-input-limitations.html

Giới hạn khung nhập liệu với RegEx © JavaScriptBank.comThêm một hiệu ứng JavaScript nữa để bạn có thể dễ dàng kiểm tra và hạn chế số lượng kí tự mà người dùng có thể nhập vào các biểu mẫu. Trong ví dụ mẫu JavaScript trực tiếp này, bạn sẽ không thể nhập vào hơn 20 kí tự hoặc quá 10 dòng tùy điều kiện nào đến trước.Hiệu ứng JavaScript này ưu việt hơn ở chỗ việc kiểm tra sẽ được thực hiện với các biểu thức so trùng (RegEx - Regular Expression); vì vậy việc kiểm tra khá chính xác.

Phiên bản đầy đủ: jsB@nk » Biểu mẫu » Giới hạn khung nhập liệu với RegEx
URL: https://www.javascriptbank.com/javascript-regex-example-code-text-input-limitations.html



JavaScript
<script type="text/javascript">// Created by: Ilya Gerasimenko | http://www.gerasimenko.com/// This script downloaded from www.JavaScriptBank.com// clean linescleanUpLine = function (str,limit) { // clean string var clean_pass1 = str.replace(/\W/g, ' ');//replace punctuation with spaces var clean_pass2 = clean_pass1.replace(/\s{2,}/g, ' ');// compact multiple whitespaces var clean_pass3 = clean_pass2.replace(/^\s+|\s+$/g, '');// trim whitespaces from beginning or end of string var clean_pass4 = clean_pass3.substring(0,limit);// trim string return clean_pass4;}// number of keywords and keyword length validationcleanUpList = function (fld) { var charLimit = 20;// ADJUST: number of characters per line var lineLimit = 10;// ADJUST: number of lines var cleanList = []; var re1 = /\S/;// all non-space characters  var re2 = /[\n\r]/;// all line breaks var tempList = fld.value.split('\n'); for (var i=0; i<tempList.length;i++) {  if (re1.test(tempList[i])) { // store filtered lines in an array   var cleanS = cleanUpLine(tempList[i],charLimit);   cleanList.push(cleanS);  }  }  for (var j=0; j<tempList.length;j++) {  if (tempList[j].length > charLimit && !re2.test(tempList[j].charAt(charLimit))) { // make sure that last char is not a line break - for IE compatibility  fld.value = cleanList.join('\n'); // restore from array  } } if (cleanList.length > lineLimit) {  cleanList.pop();// remove last line  fld.value = cleanList.join('\n'); // restore from array } fld.onblur = function () {this.value = cleanList.join('\n');} // onblur - restore from array}// Multiple onload function created by: Simon Willison// http://simonwillison.net/2004/May/26/addLoadEvent/function addLoadEvent(func) {  var oldonload = window.onload;  if (typeof window.onload != 'function') {    window.onload = func;  } else {    window.onload = function() {      if (oldonload) {        oldonload();      }      func();    }  }}addLoadEvent(  function () {  document.form.word_list.onkeyup =function () {  cleanUpList(document.form.word_list);  }  });</script>


HTML
Textarea below has limitations: 20 characters and 10 lines<br /><form name="form"> <textarea cols="22" rows="12" name="word_list" id="word_list"></textarea></form>