?? validateutil.js
字號:
/**
* 判斷文本輸入框的值是否全部大寫字母和數字組成【字母范圍A-IV】【數字范圍1-65536】
* @param objID文本框的id
* @param message 警告框里的值
* @return false 并出現提醒框
*/
function checkInputValueIsExcelCell(objID,message){
var objValue = document.getElementById(objID).value;
var re = /[^A-Z0-9]/g
//alert(objValue);
//alert(re.test(objValue));
if(re.test(objValue)){
alert(message);
document.getElementById(objID).focus();
document.getElementById(objID).select();
return false;
}else{
return true;
}
}
/**
* 判斷文本輸入框的值是否全部為字母、數字和小數點組成
* @param objID文本框的id
* @param message 警告框里的值
* @return false 并出現提醒框
*/
function checkInputValueIsCharOrNumber2(objID,message){
var objValue = document.getElementById(objID).value;
var re = /[^A-Za-z0-9.]/g
//alert(objValue);
//alert(re.test(objValue));
if(re.test(objValue)){
alert(message);
document.getElementById(objID).focus();
document.getElementById(objID).select();
return false;
}else{
return true;
}
}
/**
* 判斷文本輸入框的值是否全部為字母、下劃線或數字組成
* @param objID文本框的id
* @param message 警告框里的值
* @return false 并出現提醒框
*/
function checkInputValueIsCharOrNumber(objID,message){
var objValue = document.getElementById(objID).value;
var re = /[^A-Za-z0-9_]/g
//alert(objValue);
//alert(re.test(objValue));
if(re.test(objValue)){
alert(message);
document.getElementById(objID).focus();
document.getElementById(objID).select();
return false;
}else{
return true;
}
}
/**
* 去除字符串左邊空格、右邊空格和兩邊空格
* @param str是要驗證的文本框的值
* @return 返回去掉空格后的文本框的值
*/
function trim(str){ //刪除左右兩端的空格
return str.replace(/(^\s*)|(\s*$)/g, "");
}
function ltrim(str){ //刪除左邊的空格
return str.replace(/(^\s*)/g,"");
}
function rtrim(str){ //刪除右邊的空格
return str.replace(/(\s*$)/g,"");
}
/**
* 驗證文本框的值是否為空值
* @param objID文本框的id
* @param message 警告框里的值
* @return false 并出現提醒框
*/
function checkInputValueIsNull(objID,message){
var objValue = document.getElementById(objID).value;
if(objValue=="" || objValue == null){
alert(""+message+"的值為空,請您輸入!");
document.getElementById(objID).focus();
document.getElementById(objID).select();
return false;
}else{
document.getElementById(objID).value = trim(objValue);
return true;
}
}
/**
* validateUtil.js是一個通用的驗證,包括以下方面
* 1.空值和空格的驗證
* 2.日期的驗證
* 3.電子郵件地址的驗證
* 4.身份證號碼的驗證
* 5.整數數字的驗證
* 6.電話號碼的驗證
* 7.時間的驗證
* 如果大家有比較好的驗證都可以在這里匯總
*/
/*===========================【空值和空格的驗證】開始==============================*/
/**
* 空值驗證我分為下面幾個階段:
* 1.檢查是否為null值和空值
* 2.檢查前后是否有空格
* 3.檢查所有看是否有空格
* 4.替換前后的空格為空值
* 5.替換所有的空格為空值
* 6.根據情況進行應用
*/
/**
* 檢查輸入字符串中是否為null值或者空字符串
* @param {objID} 對象的ID
* @return true OR false;
* @modify by @tombo
*/
function checkIsNull(objID){
var objValue = document.getElementById(objID).value;
if(objValue=="" || objValue == null){
return true;
}
return false;
}
/**
* 定義一個像java一樣的js_trim()函數,替換前后【空格】為空字符串,包括【全角空格】
* @param {objID} 對象的ID
* @return string
* @modify by @tombo
*/
function js_trim(objID){
var str = document.getElementById(objID).value;
str += "";
while( (str.charAt(0)==' ')||(str.charAt(0)==' ')||(escape(str.charAt(0))=='%u3000') )
str=str.substring(1,str.length);
while( (str.charAt(str.length-1)==' ')||(str.charAt(str.length-1)==' ')||(escape(str.charAt(str.length-1))=='%u3000') )
str=str.substring(0,str.length-1);
return str;
}
/**
* 檢查輸入字符串中前后是否有空格
* @param {objID} 對象的ID
* @return true OR false;
* @modify by @tombo
*/
function checkSpaceQH(objID) {
var objValue = document.getElementById(objID).value;
var flag = false;
var i, ch;
//檢查輸入的值開頭是否有空值,\u3000是全角空格的unicode編碼
for (i = 0; i < 1; i++) {
ch = objValue.charAt(i);
if (ch == " " || ch == "\u3000") {
flag = true;
break;
}
}
//檢查輸入的值結束是否有空值,\u3000是全角空格的unicode編碼
for (i = objValue.length-1; i < objValue.length; i++) {
ch = objValue.charAt(i);
if (ch == " " || ch == "\u3000") {
flag = true;
break;
}
}
return flag;
}
/**
* 檢查輸入整個字符串中是否有空格
* @param {objID} 對象的ID
* @return true OR false;
* @modify by @tombo
*/
function checkSpaceAll(objID) {
var objValue = document.getElementById(objID).value;
var flag = false;
var i, ch;
//檢查輸入的值中是否有空值,\u3000是全角空格的unicode編碼
for (i = 0; i < objValue.length; i++) {
ch = objValue.charAt(i);
if (ch == " " || ch == "\u3000") {
flag = true;
break;
}
}
return flag;
}
/**
* 字符串中前后有空格,替換為空值
* @param {objID} 對象的ID
* @return true OR false;
* @modify by @tombo
*/
function replaceSpaceQH(objID) {
//var objValue = document.getElementById(objID).value;
var str = js_trim(objID);
return str;
//document.getElementById(objID).value = str;
//alert("--"+str+"--");
}
/**
* 替換所有的空格為空值
* @param {objID} 對象的ID
* @return true OR false;
* @modify by @tombo
*/
function replaceSpaceAll(objID) {
//var objValue = document.getElementById(objID).value;
//alert("--"+objValue+"--");
var str = js_trim(objID);
str=str.replace(/ /g,"");//半角空格
str=str.replace(/ /g,"");//全角空格
return str;
//document.getElementById(objID).value = str;
//alert("--"+str+"--");
}
/**
* 下面是判斷null值或空值、前后是否有空格;如果前后有空格不替換,而是出現警告信息
* @param {objID} 對象的ID
* @param message 對象的名稱
* @modify by @tombo
*/
function alertBlankValidateQH(objID,message) {
if(checkIsNull(objID)) {
alert("【"+message+"】的值為空,請您輸入!");
document.getElementById(objID).focus();
document.getElementById(objID).select();
return false;
}else{
if(checkSpaceQH(objID)) {
alert("您輸入的【"+message+"】中有空格存在!");
document.getElementById(objID).focus();
document.getElementById(objID).select();
return false;
}
}
}
/**
* 下面是判斷null值或空值、前后是否有空格;如果前后有空格直接替換,不出現警告信息
* @param {objID} 對象的ID
* @param message 對象的名稱
* @modify by @tombo
*/
function replaceBlankValidateQH(objID,message) {
if(checkIsNull(objID)) {
alert("【"+message+"】的值為空,請您輸入!");
document.getElementById(objID).focus();
document.getElementById(objID).select();
return false;
}else{
document.getElementById(objID).value = replaceSpaceQH(objID);
return true;
}
}
/**
* 下面是判斷null值或空值、是否有空格;如果有空格不替換,而是出現警告信息
* @param {objID} 對象的ID
* @param message 對象的名稱
* @modify by @tombo
*/
function alertBlankValidateAll(objID,message) {
if(checkIsNull(objID)) {
alert("【"+message+"】的值為空,請您輸入!");
document.getElementById(objID).focus();
document.getElementById(objID).select();
return false;
}else{
if(checkSpaceAll(objID)) {
alert("您輸入的【"+message+"】中有空格存在!");
document.getElementById(objID).focus();
document.getElementById(objID).select();
return false;
}
}
}
/**
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -