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

In - Kiểm tra dữ liệu - JavaScriptBank.com

Phiên bản đầy đủ: jsB@nk » Biểu mẫu » Hạn chế » Kiểm tra dữ liệu
URL: https://www.javascriptbank.com/universal-form-validator.html

Kiểm tra dữ liệu © JavaScriptBank.comHiệu ứng là một biểu mẫu với chức năng kiểm tra dữ liệu dạng text, số, radio.

Phiên bản đầy đủ: jsB@nk » Biểu mẫu » Hạn chế » Kiểm tra dữ liệu
URL: https://www.javascriptbank.com/universal-form-validator.html



JavaScript
<script language="javascript">// Created by: Amit Wadhwa :: http://amitwadhwa.fcpages.com/javascript.com/formvalidator.htmlfunction checkThisForm(formname, submitbutton, errors) {  if (errors == '') {    eval(formname+'.'+submitbutton+'.disabled=true');    eval('document.'+formname+'.submit()');  } else {    alert(errors);  }}function checkText(formname, textboxname, displaytext) {  var localerror = '';  if(Trim(eval('document.'+formname+'.'+textboxname+'.value'))=='') {    localerror =  '- '+displaytext+' is Required.\n';  } else localerror = '';  return localerror;}function checkNum(formname, textboxname, displaytext) {  var localerror = '';  if(isNaN(eval('document.'+formname+'.'+textboxname+'.value'))) {    localerror =  '- '+displaytext+' Should Be A Number With No Spaces.\n';  } else localerror = '';  return localerror;}function checkSpaces(formname, textboxname, displaytext) {  var valid = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_'; // define valid characters  var localerror = '';  if(!isValid(Trim(eval('document.'+formname+'.'+textboxname+'.value')), valid)) {    localerror =  '- '+displaytext+' Should Not Contain Spaces.\n';  } else localerror = '';  return localerror;}function checkSelect(formname, selectboxname, displaytext) {  var localerror = '';  if(eval('document.'+formname+'.'+selectboxname+'.selectedIndex')==0) {    localerror =  '- '+displaytext+' is Required.\n';  } else localerror = '';  return localerror;}function getRadio(formname, radioname, displaytext) {  for (var i=0; i < eval('document.'+formname+'.'+radioname+'.length'); i++) {    if (eval('document.'+formname+'.'+radioname+'[i].checked')) {      var rad_val = eval('document.'+formname+'.'+radioname+'[i].value');      return rad_val;    }  }}function checkRadio(formname, radioname, displaytext) {  var localerror = '';  var rad_val    = '';  for (var i=0; i < eval('document.'+formname+'.'+radioname+'.length'); i++) { //check every radio button by that name    if (eval('document.'+formname+'.'+radioname+'[i].checked'))  { //if it is checked      rad_val += '-';      }else rad_val += '';      }    if (rad_val=='') {      localerror =  '- '+displaytext+' is Required.\n';    }  return localerror;}function autoComplete (field, select, property) {/*onKeyUp="autoComplete(this,this.form.selectboxname,'value',false)" - add this to textbox where you are typing*/  var found = false;  for (var i = 0; i < select.options.length; i++) {    if (select.options[i][property].toUpperCase().indexOf(field.value.toUpperCase()) == 0) {      found=true; break;    }  }  if (found) {    select.selectedIndex = i;  } else {    select.selectedIndex = -1;  }  if (field.createTextRange) {    if (!found) {      field.value=field.value.substring(0,field.value.length-1);      return;    }    var cursorKeys ="8;46;37;38;39;40;33;34;35;36;45;";    if (cursorKeys.indexOf(event.keyCode+";") == -1) {      var r1 = field.createTextRange();      var oldValue = r1.text;      var newValue = found ? select.options[i][property] : oldValue;      if (newValue != field.value) {        field.value = newValue;        var rNew = field.createTextRange();        rNew.moveStart('character', oldValue.length) ;        rNew.select();      }    }  }}function Trim(s) {  while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r')) {    s = s.substring(1,s.length);  }  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r')) {    s = s.substring(0,s.length-1);  }  return s;}function isValid(string,allowed) {//  var valid = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; // define valid characters    for (var i=0; i< string.length; i++) {      if (allowed.indexOf(string.charAt(i)) == -1) return false;    }    return true;}function check(formname, submitbutton) {  var errors = '';  errors += checkText(formname, 'textfield1', 'Name');  errors += checkSelect(formname, 'Country', 'Country Of Residence');  errors += checkText(formname, 'age', 'Age Of Person');  errors += checkNum(formname, 'age', 'Age Of Person');  errors += checkRadio(formname, 'Gender', 'Your gender');  checkThisForm(formname, submitbutton, errors);}//--></script><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->


HTML
<form name="form1">  <p>Enter Name: <input type="text" name="textfield1"> (Mandatory)</p>  <p>Enter Country of Residence:    <select name="Country">      <option value="" selected>- Select Country -</option>      <option value="India">India</option>      <option value="China">China</option>      <option value="UK">UK</option>      <option value="USA">USA</option>    </select>    (Mandatory)</p>  <p>Enter Age: <input type="text" name="age"> (Mandatory, Must be a number)</p>  <p> Gender    <input type="radio" name="Gender" value="M"> Male    <input type="radio" name="Gender" value="F"> Female (Mandatory - select one)</p>  <p>    <input type="button" name="Submit" value="submit" onClick="check('form1', this.name);"></p></form><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->