/*
------------------------------------------------ ----------------------------------
File name: check.js
Description: JavaScript script, used to check the input of the web page submission form Data
version: 1.0
*/
/*
Purpose: Verify the format of the ip address
Input: strIP: ip address
Return: If it passes the verification, it returns true, otherwise it returns false;
*/
function isIP(strIP) {
if (isNull(strIP)) return false;
var re=/^(d+).(d+).(d+).(d+)$/g // Regular expression matching IP address
if(re.test(strIP))
{
if( RegExp.$1 <256 && RegExp.$2<256 && RegExp.$3<256 && RegExp.$4<256) return true;
}
return false;
}
/*
Purpose: Check whether the input string is empty or all spaces
. Input: str
Return:
If it is all empty, return true, otherwise return false
*/
function isNull( str ){
if ( str == "" ) return true;
var regu = "^[ ]+$";
var re = new RegExp(regu);
return re.test(str);
}
/*
Purpose: Check whether the value of the input object conforms to the integer format
. Input: str The input string
returns: true if it passes the verification, otherwise false
*/
function isInteger( str ){
var regu = /^[-]{0,1}[0-9]{1,}$/;
return regu.test(str);
}
/*
Purpose: Check whether the entered mobile phone number
is entered correctly:
s: string
return:
true if it passes verification, otherwise false
*/
function checkMobile( s ){
var regu =/^[1][3][0-9]{9}$/;
var re = new RegExp(regu);
if (re.test(s)) {
return true;
}else{
return false;
}
}
/*
Purpose: Check whether the input string conforms to the positive integer format
. Input:
s: string
return:
true if passed verification, otherwise false
*/
function isNumber( s ){
var regu = "^[0-9]+$";
var re = new RegExp(regu);
if (s.search(re) != -1) {
return true;
} else {
return false;
}
}
/*
Purpose: Check whether the input string is in a digital format with decimals, it can be a negative
input:
s: string
return:
true if it passes verification, otherwise false
*/
function isDecimal( str ){
if(isInteger(str)) return true;
var re = /^[-]{0,1}(d+)[.]+(d+)$/;
if (re .test(str)) {
if(RegExp.$1==0&&RegExp.$2==0) return false;
return true;
} else {
return false;
}
}
/*
Purpose: Check whether the value of the input object conforms to the port number format
. Input: str. The input string
returns: true if it passes verification, otherwise false.
*/
function isPort( str ){
return (isNumber(str) && str<65536);
}
/*
Purpose: Check whether the value of the input object conforms to the E-Mail format
Input: str The input string
returns: true if it passes verification, otherwise false
*/
function isEmail( str ){
var myReg = /^[-_A-Za-z0-9]+@([_A-Za-z0-9]+.)+[A-Za-z0-9]{ 2,3}$/;
if(myReg.test(str)) return true;
return false;
}
/*
Purpose: Check whether the input string conforms to the amount format.
The format is defined as a positive number with a decimal point, and up to three digits can
be entered after the decimal point:
s: string
return:
true if the verification is passed, otherwise false
*/
function isMoney( s ){
var regu = "^[0-9]+[.][0-9]{0,3}$";
var re = new RegExp(regu);
if (re.test (s)) {
return true;
} else {
return false;
}
}
/*
Purpose: Check whether the input string only consists of English letters, numbers and underscores
. Input:
s: string
return:
return true if it passes verification, otherwise return false
*/
function isNumberOr_Letter( s ){//Determine whether it is a number or letter
var regu = "^[0-9a-zA-Z_]+$";
var re = new RegExp(regu);
if (re.test(s)) {
return true;
}else{
return false;
}
}
/*
Purpose: Check whether the input string only consists of English letters and numbers.
Input:
s: string
return:
true if it passes verification, otherwise false
*/
function isNumberOrLetter( s ){//Determine whether it is a number or letter
var regu = "^[0-9a-zA-Z]+$";
var re = new RegExp(regu);
if (re.test(s)) {
return true;
}else{
return false;
}
}
/*
Purpose: Check whether the input string only consists of Chinese characters, letters, and numbers.
Input:
value: String
Return:
true if passed verification, otherwise false
*/
function isChinaOrNumbOrLett( s ){//Determine whether it is composed of Chinese characters, letters, and numbers
var regu = "^[0-9a-zA-Zu4e00-u9fa5]+$";
var re = new RegExp(regu);
if (re.test(s)) {
return true;
}else{
return false ;
}
}
/*
Purpose: Determine whether it is a date
input: date: date; fmt: date format
Return: if it passes verification, return true, otherwise return false
*/
function isDate(date, fmt) {
if (fmt==null) fmt="yyyyMMdd ";
var yIndex = fmt.indexOf("yyyy");
if(yIndex==-1) return false;
var year = date.substring(yIndex,yIndex+4);
var mIndex = fmt.indexOf("MM") ;
if(mIndex==-1) return false;
var month = date.substring(mIndex,mIndex+2);
var dIndex = fmt.indexOf("dd");
if(dIndex==-1) return false;
var day = date.substring(dIndex,dIndex+2);
if(!isNumber(year)||year>"2100" || year< "1900") return false;
if(!isNumber(month)||month>" 12" || month< "01") return false;
if(day>getMaxDay(year,month) || day< "01") return false;
return true;
}
function getMaxDay(year,month) {
if(month==4||month==6||month==9||month==11)
return "30";
if(month==2)
if(year%4 ==0&&year%100!=0 || year%400==0)
return "29";
else
return "28";
return "31";
}
/*
Purpose: Whether character 1 ends with string 2
Input: str1: string; str2: included string
Return: if it passes verification, return true, otherwise return false
*/
function isLastMatch(str1,str2)
{
var index = str1.lastIndexOf(str2);
if(str1.length==index+str2.length) return true;
return false;
}
/*
Purpose: Whether character 1 starts with string 2
input: str1: string; str2: included string
Return: if it passes verification, return true, otherwise return false
*/
function isFirstMatch(str1,str2)
{
var index = str1.indexOf(str2);
if(index==0) return true;
return false;
}
/*
Purpose: Character 1 is a containing string 2
Input: str1: string; str2: included string
Return: if it passes the verification, it returns true, otherwise it returns false
*/
function isMatch(str1,str2)
{
var index = str1.indexOf(str2);
if(index==-1) return false;
return true;
}
/*
Purpose: Check whether the entered start and end dates are correct. The rule is that the format of the two dates is correct,
and the end is as scheduled >= start date
input:
startDate: start date, string
endDate: end as scheduled, string
return:
if passed Verification returns true, otherwise returns false
*/
function checkTwoDate( startDate,endDate) {
if( !isDate(startDate) ) {
alert("The start date is incorrect!");
return false;
} else if( !isDate(endDate) ) {
alert("End date Incorrect!");
return false;
} else if( startDate > endDate ) {
alert("The start date cannot be greater than the end date!");
return false;
}
return true;
}
/*
Purpose: Check whether the entered Email mailbox format
is entered correctly:
strEmail: string
return:
true if passed verification, otherwise false
*/
function checkEmail(strEmail) {
//var emailReg = /^[_a-z0-9]+@([_a-z0-9]+.)+[a-z0-9]{2,3}$ /;
var emailReg = /^[w-]+(.[w-]+)*@[w-]+(.[w-]+)+$/;
if( emailReg.test (strEmail) ){
return true;
}else{
alert("The email address format you entered is incorrect!");
return false;
}
}
/*
Purpose: Check whether the entered phone number format is correct
. Enter:
strPhone: string
return:
true if it passes verification, otherwise false
*/
function checkPhone( strPhone ) {
var phoneRegWithArea = /^[0][1-9]{2,3}-[0-9]{5,10}$/;
var phoneRegNoArea = /^[1-9] {1}[0-9]{5,8}$/;
var prompt = "The phone number you entered is incorrect!"
if( strPhone.length > 9 ) {
if( phoneRegWithArea.test(strPhone) ){
return true ;
}else{
alert( prompt );
return false;
}
}else{
if( phoneRegNoArea.test( strPhone ) ){
return true;
}else{
alert( prompt );
return false;
}
}
}
/*
Purpose: Check the number of selected check boxes.
Input:
checkboxID: string.
Return:
Return the number of selected check boxes.
*/
function checkSelect( checkboxID ) {
var check = 0;
var i=0;
if( document.all(checkboxID).length > 0 ) {
for( i=0; i<document.all(checkboxID).length; i++ ) {
if( document.all(checkboxID).item( i ).checked ) {
check += 1;
}
}
}else{
if( document.all(checkboxID).checked )
check = 1;
}
return check;
}
function getTotalBytes(varField) {
if(varField == null)
return -1;
var totalCount = 0;
for (i = 0; i< varField.value.length; i++) {
if (varField.value.charCodeAt(i) > 127)
totalCount += 2;
else
totalCount++ ;
}
return totalCount;
}
function getFirstSelectedValue( checkboxID ){
var value = null;
var i=0;
if( document.all(checkboxID).length > 0 ){
for( i=0; i<document.all(checkboxID).length; i++ ){
if( document.all(checkboxID).item( i ).checked ){
value = document.all(checkboxID).item(i).value;
break;
}
}
} else {
if( document.all(checkboxID).checked )
value = document.all(checkboxID).value;
}
return value;
}
function getFirstSelectedIndex( checkboxID ){
var value = -2;
var i=0;
if( document.all(checkboxID).length > 0 ){
for( i=0; i<document.all(checkboxID).length; i++ ) {
if( document.all(checkboxID).item( i ).checked ) {
value = i;
break;
}
}
} else {
if( document.all(checkboxID).checked )
value = -1;
}
return value;
}
function selectAll( checkboxID,status ){
if(document.all(checkboxID) == null)
return;
if( document.all(checkboxID).length > 0 ){
for( i=0; i<document.all(checkboxID).length; i++ ){
document.all(checkboxID).item( i ).checked = status;
}
} else {
document.all(checkboxID).checked = status;
}
}
function selectInverse( checkboxID ) {
if( document.all(checkboxID) == null)
return;
if( document.all(checkboxID).length > 0 ) {
for( i=0; i<document.all(checkboxID).length; i++ ) {
document.all(checkboxID).item( i ).checked = !document .all(checkboxID).item( i ).checked;
}
} else {
document.all(checkboxID).checked = !document.all(checkboxID).checked;
}
}
function checkDate( value ) {
if(value=='') return true;
if(value.length!=8 || !isNumber(value)) return false;
var year = value.substring(0,4);
if( year>"2100" || year< "1900")
return false;
var month = value.substring(4,6);
if(month>"12" || month< "01") return false;
var day = value.substring(6,8);
if(day>getMaxDay(year,month) || day< "01") return false;
return true;
}
/*
Purpose: Check whether the entered start and end dates are correct. The rule is that the format of the two dates is correct or both are empty
and the end date >= start date.
Input:
startDate: start date, string
endDate: end date, string
return :
Returns true if the verification passes, otherwise returns false
*/
function checkPeriod( startDate,endDate) {
if( !checkDate(startDate) ) {
alert("The start date is incorrect!");
return false;
} else if( !checkDate(endDate) ) {
alert("End date Incorrect!");
return false;
} else if( startDate > endDate ) {
alert("The start date cannot be greater than the end date!");
return false;
}
return true;
}
/*
Purpose: Check whether the security code
is entered correctly:
secCode: security code
return:
true if the verification is passed, otherwise false
*/
function checkSecCode( secCode ) {
if( secCode.length !=6 ){
alert("The security code length should be 6 digits");
return false;
}
if(!isNumber( secCode ) ){
alert("Security codes can only contain numbers");
return false;
}
return true;
}
/****************************************************** ***
function:cTrim(sInputString,iType)
description: Function to remove spaces from string
parameters: iType: 1=Remove spaces on the left side of string
2=Remove spaces on the left side of the string
0=Remove spaces on the left and right sides of the string
return value: String with spaces removed
************************ ******************************/
function cTrim(sInputString,iType)
{
var sTmpStr = ' ';
var i = -1;
if(iType == 0 || iType == 1)
{
while(sTmpStr == ' ')
{
++i;
sTmpStr = sInputString.substr(i,1);
}
sInputString = sInputString.substring(i);
}
if(iType == 0 || iType == 2)
{
sTmpStr = ' ';
i = sInputString.length;
while(sTmpStr == ' ')
{
--i;
sTmpStr = sInputString.substr(i,1);
}
sInputString = sInputString.substring(0,i+1);
}
return sInputString;
}
/*
------------------------------------------------ ----------------------------------
Description: JavaScript script, verify the data items in the form begin
------- -------------------------------------------------- -----------------------
*/
function checkForm(objFrm){
var len = 0;
len = objFrm.elements.length;
var i = 0;
var objCheck;
//Text box
for(i = 0; i < len; i ++){
objCheck = objFrm.elements[i];
if(objCheck.type == "text" && !f_checkTextValid( objCheck) ){
return false;
}
}
//Drop-down box
for(i = 0; i < len; i ++){
objCheck = objFrm.elements[i];
if(objCheck.type ==select-one" && !f_checkSelectValid(objCheck) ){
return false;
}
}
//The time period is valid
if( f_checkStartAndEndDate(objFrm) == false) return false;
return true;
}
function f_checkSelectValid(obj){
//alert("check select");
if(obj.options.length <= 0){
alert("No data in drop-down selection box!");
return false;
}
return true;
}
function f_checkStartAndEndDate(frm){
var len = frm.elements.length;
if(len == null && len == 0) return true;
var i=0;
var temp;
var objCheck;
var objStartDate;
var objEndDate;
//alert("start date period check");
try{
for(i=0; i< len ; i++){
objCheck = frm.elements[i];
temp = objCheck.name;
if( temp.indexOf( "startDate") >0 ||temp.indexOf("beginDate")>0 )
objStartDate = objCheck;
if( temp.indexOf("endDate") > 0 )
objEndDate = objCheck;
}
//alert(objStartDate.value);
//alert(objEndDate.value);
if(objStartDate.value==null || objStartDate.value ==" || objStartDate.value ==null || objStartDate.value = =""){
return true;
}
return checkTwoDate(objStartDate.value, objEndDate.value);
//alert("end date period check");
}catch(E){}
return true;
}
function f_checkTextValid(obj){
//Cannot be empty
if(obj.getAttribute("isNeed") != null){
if(f_isNotNull(obj) == false) return false;
}
//Cannot exceed the length
if(obj.getAttribute ("maxlength") != null){
if(f_checkLength(obj) == false) return false;
}
var checkType ="";
checkType = obj.getAttribute("checkType");
if(checkType==null||checkType == "") return true;
//
if (checkType.indexOf("number") >=0){
if(f_isNumber(obj) == false) return false;
if(f_checkNumType(obj,checkType) == false) return false;
}
//
if (checkType.indexOf("positive") >=0){
if(f_isNumber(obj) == false) return false;
if(f_isPositive(obj)==false) return false;
if(f_checkNumType(obj, checkType) == false) return false;
}
if (checkType.indexOf("date") >=0){
if(f_checkDate(obj) == false) return false;
}
/*
switch(checkType){
case "number": if(f_isNumber(obj) == false) return false;break;
case "date": if(f_checkDate(obj) == false) return false;break;
default:
}
*/
return true;
}
function f_isNotNull(obj){
if(obj.value == ""){
f_alert(obj, "Null values are not allowed!");
return false;
}
return true;
}
function f_isNumber(obj){
if(isNaN(obj.value)){
f_alert(obj,"should be a numeric type");
return false;
}
return true;
}
function f_checkDate(obj) {
if(checkDate(obj.value) ==false){
f_alert(obj,"Not a legal date format!");
return false;
}
return true;
}
function f_checkLength(obj){
if(getTotalBytes(obj) > Math.abs( obj.getAttribute("maxlength") ) ){
f_alert(obj,"Length limit exceeded!");
return false;
}
return true;
}
function f_alert(obj,alertStr){
var fielName = obj.getAttribute("fieldName");
if(fielName == null)
fielName = "";
alert(fielName + "n" +alertStr);
obj.select();
obj.focus();
}
function f_checkNumType(obj, numType){
//Assumption: Numeric type judgment has been carried out
var strTemp;
var numpric;
var numLen;
var strArr;
var defaultLen = 19;
var defaultpric = 5;
try{
if(numType == null|| numType =="") return f_checkNumLenPrec(obj,defaultLen, defaultpric);
if(numType.indexOf("(") < 0 || numType.indexOf(")") < 0 ) return f_checkNumLenPrec(obj,defaultLen, defaultpric);
strTemp = numType.substr( numType.indexOf("(") + 1 ,numType.indexOf(")") - numType.indexOf("(") -1 );
if (strTemp == null||strTemp =="") return f_checkNmLenPrec(obj,defaultLen, defaultpric);
strArr = strTemp.split(",");
numLen = Math.abs( strArr[0] );
numpric = Math.abs( strArr[1] );
return f_checkNumLenPrec(obj,numLen, numpric);
}catch(e){
alert("in f_checkNumType = " + e);
return f_checkNumLenPrec(obj,defaultLen, defaultpric);
}
}
function f_checkNumLenPrec(obj, len, pric){
var numReg;
var value = obj.value;
var strValueTemp, strInt, strDec;
//alert(value + "=====" + len + "====="+ pric);
try{
numReg =/[-]/;
strValueTemp = value.replace(numReg, "");
strValueTemp = strValueTemp.replace(numReg, "");
//integer
if(pric==0){
numReg =/[. ]/;
//alert(numReg.test(value));
if(numReg.test(value) == true){
f_alert(obj, "The input must be an integer type!");
return false;
}
}
if(strValueTemp.indexOf(".") < 0 ){
//alert("lennth==" + strValueTemp);
if(strValueTemp.length >(len - pric)){
f_alert(obj, "The number of integer digits cannot exceed "+ (len - pric) +" bits");
return false;
}
}else{
strInt = strValueTemp.substr( 0, strValueTemp.indexOf(".") );
//alert("lennth==" + strInt);
if(strInt.length >(len - pric)){
f_alert(obj, "The number of integer digits cannot exceed "+ (len - pric) +" bits");
return false;
}
strDec = strValueTemp.substr( (strValueTemp.indexOf(".")+1), strValueTemp.length );
//alert("pric==" + strDec);
if(strDec.length > pric){
f_alert(obj, "The number of decimal places cannot exceed "+ pric +" places");
return false;
}
}
return true;
}catch(e){
alert("in f_checkNumLenPrec = " + e);
return false;
}
}
function f_isPositive(obj){
var numReg =/[-]/;
if(numReg.test(obj.value) == true){
f_alert(obj, "Must be a positive number!");
return false;
}
return true ;
}
/*
function selectedCheckboxCount(form)
Function description: Count the selected options in the Form
Parameter description:
form: the specified form
*/
function selectedCheckboxCount(form){
var length =0;
var i=0;
var count =0;
eles = form.elements;
while(i<eles.length){
obj= eles. item(i);
//type = obj.attributes.item("type").nodeValue;
type = obj.type;
if(type == "checkbox"){
if(obj.checked){
count++;
}
}
i++;
}
return count;
}
//Get the byte length
function getByteLen(str)
{
var l = str.length;
var n = l;
for ( var i=0; i<l; i++ )
if ( str.charCodeAt(i) <0 || str.charCodeAt(i) >255 )
n=n+1;
return n
}
/*
Instructions:
1. Clear the data in the table (0.0 and 0)
2. If there is no data in the cell, a space will be automatically added
3. Clear the checkbox of the blank row
Parameters:
clearzero: whether to clear "0" and "0.0", false not to clear, true to clear (default is true)
tablename: name of the table to be cleared, default is sortTable
*/
function clear_table(clearzero,tablename)
{
var tobject;
if (tablename==null)
tobject=gmobj("sortTable");
else
tobject=gmobj(tablename);
//If table is not defined, no filtering will be performed
if(tobject==null)
return;
//If the function call parameter is empty, it means that 0 and 0.0 should be cleared; otherwise, 0 and 0.0 should not be cleared.
var clear = (clearzero==null)?true:clearzero;
//Clear 0, 0.0, fill in the spaces
var rows = tobject.rows;
var j=0;
for(var i=0;i<rows.length;i++)
{
//Get the attribute clear of the first cell, if 1, indicating that there is no data in this row, then clear all data in this row
while(tobject.rows[i].cells[j] != null)
{
if(clear)
{
if(tobject.rows[i].cells[j].innerHTML==0 ||tobject.rows[i]. cells[j].innerHTML==0.0||tobject.rows[i].cells[j].innerHTML=="")
tobject.rows[i].cells[j].innerText=" ";
}
else
{
if (tobject.rows[i].cells[j].innerHTML=="")
tobject.rows[i].cells[j].innerText=" ";
}
j++;
}
j=0;
}
return true;
}
function gmobj(mtxt) /* Get object by object name */
{
if (document.getElementById) {
m=document.getElementById(mtxt);
} else if (document.all) {
m=document.all[mtxt];
} else if (document.layers) {
m=document.layers[mtxt];
}
return m;
}
/*
-------------------------- -------------------------------------------------- ---
Description: JavaScript script, verify the data items in the form end
---------------------------------- --------------------------------------------------
*/
/*
Purpose: Check whether the input string is a number format with decimals, which can be a negative number (and meet the specified precision)
input: str: string
l: total number of digits
d: number of digits after the decimal point
Return:
true if passed verification , otherwise return false
*/
function isDecimal( str,l,d ){
if(isInteger(str)) {
if (l==null) return true;
if (str<0) l--;
if (str.length<=l) return true;
}
var re = /^[-]{0,1}(d+)[.]+(d+)$/;
if (re.test(str)) {
if (l==null) return true;
if (d==null) d=0;
if(RegExp.$1==0&&RegExp.$2==0) return false;
if (RegExp.$1.length+RegExp.$2.length<=l
&& RegExp.$2.length< =d) return true;
}
return false;
}
onclick="isNull('Co.PageForm.CompetitorName');"