请参看:http://hw1287789687.iteye.com/blog/2124945
(1)判断是否是数字
- /***
- * whether is digit
- */
- com.whuang.hsj.isInteger=function(int22){
- if(!int22 || int22==undefined){
- return false;
- }
- if( int22.match(/^[\d\\.]+$/i)){
- return true;
- }else{
- return false;
- }
- };
应用:
- var qrcodeSizeObj=com.whuang.hsj.$$id('qrcodeSize');
- var qrcodeSizeint=qrcodeSizeObj.value;
- if(!com.whuang.hsj.isHasValue(qrcodeSizeint)){
- qrcodeSizeObj.focus();
- alert('请输入像素大小.');
- return ;
- }
- var isDigit=com.whuang.hsj.isInteger(qrcodeSizeint);
- if(!isDigit){
- alert('请输入数字[100-1000]');
- return;
- }
(2)文本框只能输入数字
- <input type="text" id="qrcodeSize" onkeyup="onlyNumberKeyUp(event)" >像素
这样,文本框就只能输入数字了,如果输入其他字符,比如字母,就会自动删除.
onlyNumberKeyUp实现:
- /*
- * 只能输入数字
- *
- */
- onlyNumberKeyUp=function(event){
- var obj=event.srcElement?event.srcElement:event.target;
- var pattern = /[^\d\.\/]/ig;
- if(pattern.test(obj.value)) {
- var i=getCursortPosition(event);
- obj.value=obj.value.replace(pattern,'');
- setCaretPosition(event,i);
- }
- };
(3)判断输入框是否为空
- /**
- * whether has value
- *
- * @param {Object}
- * input
- */
- com.whuang.hsj.isHasValue = function(input) {
- if (typeof input == "number" && input == "0") {
- return true;
- }
- if(!input)
- {
- return false;
- }
- if(input==""||input==undefined||com.whuang.hsj.isWholeWhitespace(input)){
- return false;
- }
- return true;
- };
- /**
- * is whitespace entirely
- *
- * @param {Object}
- * inputString
- */
- com.whuang.hsj.isWholeWhitespace = function(inputString) {
- if (typeof inputString == "object") {
- return inputString;
- }
- var bootInit = true;
- if (inputString == "" || inputString == undefined) {
- return false;
- }
- for ( var i = 0; i < inputString.length; i++) {
- var c = inputString.charAt(i);
- if (!com.whuang.hsj.isWhitespace(c)) {
- bootInit = false;
- break;
- }
- }
- return bootInit;
- };
- com.whuang.hsj.isWhitespace = function(input) {// whether has whitespace
- var whitespace = " \t\n\r";
- for ( var i = 0; i < input.length; i++) {
- var c = input.charAt(i);
- if (whitespace.indexOf(c) >= 0) {
- return true;
- }
- }
- return false;
- };
应用:
- var qrcodeSizeObj=com.whuang.hsj.$$id('qrcodeSize');
- var qrcodeSizeint=qrcodeSizeObj.value;
- if(!com.whuang.hsj.isHasValue(qrcodeSizeint)){
- qrcodeSizeObj.focus();
- alert('请输入像素大小.');
- return ;
- }
(4)js动态添加组件(控件)
- var buttonTd=com.whuang.hsj.$$id("buttonTd");//td tag
- var newInput =document.createElement("input");
- newInput.type="button";
- newInput.name="input1";
- newInput.value="修改密码";
- newInput.className='class2';
- buttonTd.appendChild(newInput);
说明:newInput.className 是用于指定样式
请参考:http://hw1287789687.iteye.com/blog/2124945