- //测试输入框的值是否整数
- function checkNum(inputObj) {
- var regex = /^/d+$/;
- return regex.test(inputObj.value);
- }
- //测试输入框的值的个数
- function checkLength(inputObj, maxLength) {
- return inputObj.value.length <= maxLength;
- }
- //将字符串的首尾空格全部去掉(在页面引用该js后,可以在value.Trim(),即可以去掉改值的空格)
- String.prototype.Trim = function() {
- return this.replace(/(^/s*)|(/s*$)/g, "");
- }
- //测试输入框是否空值
- function checkInputValueIsEmpty(inputObj) {
- var s = inputObj.value.Trim();
- if (s == null || s == "") {
- return false;
- }
- else {
- return true;
- }
- }
- //验证最大长度指定的正小数,inputObj为input对象,beforeLength为小数点前面的位数个数,afterLength为小数点后面的位数个数
- function checkNumberLength(inputObj, beforeLength, afterLength) {
- if (inputObj.value.indexOf(".") >= 0) {
- var regex = new RegExp("^//d{0," + beforeLength + "}[.]?//d{0," + afterLength + "}$");
- return regex.test(inputObj.value);
- }
- else {
- var regex = new RegExp("^//d{0," + beforeLength + "}$");
- return regex.test(inputObj.value);
- }
- }
- //验证最大长度指定的正负小数,inputObj为input对象,beforeLength为小数点前面的位数个数,afterLength为小数点后面的位数个数
- function checkAllNumberLength(inputObj, beforeLength, afterLength) {
- if (inputObj.value.indexOf(".") >= 0) {
- var regex = new RegExp("^-?//d{0," + beforeLength + "}[.]?//d{0," + afterLength + "}$");
- return regex.test(inputObj.value);
- }
- else {
- var regex = new RegExp("^-?//d{0," + beforeLength + "}$");
- return regex.test(inputObj.value);
- }
- }
- //测试输入框的值是否带小数
- function checkNumber(inputObj) {
- var regex = /^([0-9]*)([.]?)([0-9]*)$/;
- return regex.test(inputObj.value);
- }
- //测试输入框的值是否小数
- function checkAllNumber(inputObj) {
- var regex = /^-?([0-9]*)([.]?)([0-9]*)$/;
- return regex.test(inputObj.value);
- }
- //测试身份证验证
- function checkIDnumber(inputObj) {
- var regex = /(^/d{15}$)|(^/d{17}([0-9]|X)$)/;
- return regex.test(inputObj.value);
- }
- //测试身份证验证
- function checkemail(inputObj) {
- var regex = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
- return regex.test(inputObj.value);
- }
- //输入最大长度的正小数,inputObj为input对象,beforeLength为小数点前面的位数个数,afterLength为小数点后面的位数个数
- function KeyPress(inputObj, beforeLength, afterLength) {//只允许录入数据字符 0-9 和小数点
- var keyvalue = String.fromCharCode(event.keyCode); //根据keyCode获取keyValue
- var inputValue = inputObj.value + keyvalue;
- var testValue = false;
- if (inputValue.indexOf(".") >= 0) {
- var regex = new RegExp("^//d{0," + beforeLength + "}[.]?//d{0," + afterLength + "}$");
- testValue = regex.test(inputValue);
- }
- else {
- var regex = new RegExp("^//d{0," + beforeLength + "}$");
- testValue = regex.test(inputValue);
- }
- if (!testValue) {
- event.returnValue = false;
- }
- }
- //输入最大长度的正负小数,inputObj为input对象,beforeLength为小数点前面的位数个数,afterLength为小数点后面的位数个数
- function KeyPressAllNumber(inputObj, beforeLength, afterLength) {//只允许录入所有小数
- var keyvalue = String.fromCharCode(event.keyCode); //根据keyCode获取keyValue
- var inputValue = inputObj.value + keyvalue;
- var testValue = false;
- if (inputValue.indexOf(".") >= 0) {
- var regex = new RegExp("^-?//d{0," + beforeLength + "}[.]?//d{0," + afterLength + "}$");
- testValue = regex.test(inputValue);
- }
- else {
- var regex = new RegExp("^-?//d{0," + beforeLength + "}$");
- testValue = regex.test(inputValue);
- }
- if (!testValue) {
- event.returnValue = false;
- }
- }
- //输入整数
- function KeyPressNum(objTR) {//只允许录入数据字符 0-9
- //var objTR = element.document.activeElement;
- var txtval = objTR.value;
- var key = event.keyCode;
- if (key < 48 || key > 57) {
- event.keyCode = 0;
- }
- }
- function checkInputValueIsEmptyOrOverLength(inputObj, inputLength, inputField) {
- if (!checkInputValueIsEmpty(inputObj)) {
- alert(inputField + "不能为空!");
- return false;
- }
- if (!checkLength(inputObj, inputLength)) {
- alert(inputField + "输入值超出最大长度!");
- return false;
- }
- return true;
- }
- //将inputObj的文本全角转半角
- function DBC2SBC(inputObj) {
- //如果是左右方向键,直接返回
- if (event.keyCode == 37 || event.keyCode == 39) {
- return;
- }
- var result = "";
- for (var i = 0; i < inputObj.value.length; i++) {
- code = inputObj.value.charCodeAt(i); //获取当前字符的unicode编码
- if (code >= 65281 && code <= 65373)//在这个unicode编码范围中的是所有的英文字母已及各种字符
- {
- result += String.fromCharCode(inputObj.value.charCodeAt(i) - 65248);
- //把全角字符的unicode编码转换为对应半角字符的unicode码
- }
- else if (code == 12288)//空格
- {
- result += String.fromCharCode(inputObj.value.charCodeAt(i) - 12288 + 32);
- }
- else {
- result += inputObj.value.charAt(i);
- }
- }
- inputObj.value = result;
- }
- 加个按钮的样例:
- <asp:TextBox ID="txtBDDW" runat="server" οnkeypress="KeyPress(this,4,2);" CssClass="input01"
- οnfοcus="this.className='input02'" οnblur="this.className='input01'" οnkeyup="DBC2SBC(this);"></asp:TextBox><span><font
- color="red">*</font></span>只允许输入带两位小数的四位整数
- js验证样例:
- --非空验证
- if (document.getElementById('txtCwenKongZai').value.Trim() == "") {
- alert("请输入常温空载!");
- document.getElementById('txtCwenKongZai').focus();
- return false;
- }
- -- 防止数字输入,通过复制超过文本框限制长度
- if (checkNumberLength(document.getElementById('txtCwenQinzai'), 6, 2) == false) {
- alert("常温轻载格式错误!")
- document.getElementById('txtCwenQinzai').focus();
- return false;
- }
常见js验证
最新推荐文章于 2023-03-09 15:59:17 发布