Home  >  Article  >  Web Front-end  >  Some commonly used Javascript judgment functions_Basic knowledge

Some commonly used Javascript judgment functions_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:51:081070browse

1.

This is an DIV





2. Javascript error

Copy code The code is as follows:

w1=0;
w1 =Math.round(1333333.99*100 );
w1 =Math.round(13663.38*100);
document.write(w1/100);
w1=0;
w1 =1333333.99;
w1 =13663.38;
document.write(w1);

3. Form check
Copy code The code is as follows :

//wanghui added a function checkPort() in 20031020 to check the business access and processing address port number;
function toChnDigit(num)
{
var t = parseInt(num) ;
if(t==0) return "zero";
if(t==1) return "one";
if(t==2) return "two";
if (t==3) return "三";
if(t==4) return "四";
if(t==5) return "五";
if(t==6 ) return "six";
if(t==7) return "seven";
if(t==8) return "eight";
if(t==9) return "nine" ;
return "";
}
//@CheckItem@ OPT-HuTie-20031208 Optimization: Add a public function to disable all buttons
function disableAllButtons(){
for(var i= 0;ivar tmp = document.all.tags("input")[i];
if(tmp.type== "button" || tmp.type=="submit" ||tmp.type=="reset"){
tmp.disabled = true;
}
}
}
/ /Add trim function
String.prototype.trim = function()
{
return this.replace(/(^s*)|(s*$)/g, "");
}
//Function name: checkNUM
//Function introduction: Check whether it is a number
//Parameter description: The number to be checked
//Return value: 1 means it is a number, 0 means it is not Number
function checkNum(Num) {
var i,j,strTemp;
strTemp = "0123456789.";
if (Num.length == 0)
return 0
for (i = 0;i < Num.length; i ) {
j = strTemp.indexOf(Num.charAt(i));
if (j == -1) {
// Indicates that there is a character that is not a number
return 0;
}
}
//Indicates that it is a number
return 1;
}
//Function name: checkNUM
/ /Function introduction: Check whether it is a number
//Parameter description: The number to be checked
//Return value: 1 means it is a number, 0 means it is not a number
function checkIntNum(Num) {
var i,j,strTemp;
strTemp = "0123456789";
if ( Num.length == 0)
return 0
for (i = 0;i < Num.length; i ) {
j = strTemp.indexOf(Num.charAt(i));
if (j == -1) {
//Indicates that there are characters that are not numbers
return 0;
}
}
//The description is a number
return 1;
}
//Function name: checkEmail
//Function introduction: Check whether it is an Email Address
//Parameter Description: String to be checked
//Return value: 0: Not 1: Yes
function checkEmail(a) {
//@CheckItem@ Bug141-hutie-20030821 Modify interface: Email address needs to be done Disable Chinese verification
var reg = /[^u0000-u00FF]/;
if(a.match(reg)!=null){
return 0;//There are Chinese characters
}
var i=a.length;
var temp = a.indexOf('@');
var tempd = a.indexOf('.');
if (temp > 1) {
if ((i-temp) > 3){
if ((i-tempd)>0){
return 1;
}
}
}
return 0;
}
//Function name: checkTEL
//Function introduction: Check whether it is a phone number
//Parameter description: The string to be checked
//Return value : 1 means legal, 0 means illegal
function checkTel(tel)
{
var i,j,strTemp;
strTemp = "0123456789- ()";
for (i =0;i{
j = strTemp.indexOf(tel.charAt(i));
if (j==-1)
{
//Indicates that there are characters that are illegal
return 0;
}
}
//Indicates that the characters are legal
return 1;
}
//Function name: checkLength
//Function introduction: Check the length of the string
//Parameter description: The string to be checked
//Return value: Length value
function checkLength(strTemp)
{
var i ,sum;
sum = 0;
for(i=0;i{
//@CheckItem@ BUG-Renhj-20040604 Optimization: Function that will be verified Change it to 128 to use a single character.Avoid the "·" symbol
// if ((strTemp.charCodeAt(i)>=0) && (strTemp.charCodeAt(i)<=255))
if ((strTemp.charCodeAt(i) >=0) && (strTemp.charCodeAt(i)<=128))
sum = sum 1;
else
sum = sum 2;
}
return sum;
}
//Function name: checkSafe
//Function introduction: Check whether it contains "'", '"',"<", ">", ";", ","
//Parameter description: String to be checked
//Return value: 0: Yes 1: No
function checkSafe(a)
{
fibdn = new Array ("'",' "',">", "<", ",", ";");
i = fibdn.length;
j = a.length;
for (ii=0;ii< ;i;ii )
{
for (jj=0;jj{
temp1 = a.charAt(jj);
temp2 = fibdn[ii];
if (temp1==temp2)
{
return 0;
}
}
}
return 1;
}
//Function name: checkChar
//Function introduction: Check whether it contains non-alphabetic characters
//Parameter description: The string to be checked
//Return value: 0: Contains 1: All letters
function checkChar( str)
{
var strSource ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.()& ";
var ch;
var i;
var temp;
for (i=0;i<= (str.length-1);i )
{
ch = str.charAt(i);
temp = strSource.indexOf(ch);
if (temp==-1)
{
return 0;
}
}
return 1;
}
//Function name: checkCharOrDigital
//Function introduction: Check whether it contains non-digit or Letter
//Parameter description: String to be checked
//Return value: 0: Contains 1: All numbers or letters
function checkCharOrDigital(str)
{
var strSource = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.()& ";
var ch;
var i;
var temp; >{
ch = str.charAt(i);
temp = strSource.indexOf(ch);
if (temp == -1)
{
return 0;
}
}
return 1;
}
//Function name: checkChinese
//Function introduction: Check whether it contains Chinese characters
//Parameter description: String to be checked
//Return value: 0: Contains 1: No
function checkChinese(strTemp)
{
var i,sum;
for(i=0;i{
if ((strTemp.charCodeAt(i)<0) || (strTemp.charCodeAt(i)>255))
return 0;
}
return 1;
}
//Function name: compareTime()
//Function introduction: Compare time size
//Parameter description: beginYear starts year, beginMonth starts month, benginDay starts day, beginH starts hour, beginM start minute,
// endYear end year, endMonth end month, endMonth end day, endH end hour, endM end minute
//Return value: true means the start time is greater than the end time, false on the contrary
function compareTime(beginYear,beginMonth,benginDay,beginH,beginM,endYear,endMonth,endDay,endH,endM){
var date1 = new Date(beginYear,beginMonth-1,benginDay,beginH,beginM);
var date2 = new Date(endYear,endMonth-1,endDay,endH,endM);
if(date1.getTime()>date2.getTime()){
return false;
}
return true;
}
//Function name: compareDate()
//Function introduction: Compare date size
//Parameter description: beginYear starts year, beginMonth starts month, benginDay starts day
//endYear ends the year, endMonth ends the month, endMonth ends the day
//Return value: 0: true means the start time is greater than the end time, false on the contrary
function compareDate(beginYear,beginMonth,benginDay,endYear,endMonth,endDay ){
var date1 = new Date(beginYear,beginMonth-1,benginDay);
var date2 = new Date(endYear,endMonth-1,endDay);
if(date1.getTime()> date2.getTime()){
return false;
}
return true;
}
//Function name: checkUrl
//Function introduction: Check whether the Url is legal
//Parameter description: String to be checked
//Return value: true: legal false: illegal.
function checkURL(strTemp)
{
if(strTemp.length==0) return false;
if(checkChinese(strTemp)==0) return false;
if (strTemp.toUpperCase().indexOf("HTTP://") != 0 && strTemp.toUpperCase().indexOf("HTTPS://") != 0){
return false;
}
return true;
}
// @CheckItem@ OPT-Renhj-20030704 提供公共的去处空格的方法
//清除左边空格
function js_ltrim(deststr)
{
if(deststr==null)return "";
var pos=0;
var retStr=new String(deststr);
if (retStr.lenght==0) return retStr;
while (retStr.substring(pos,pos 1)==" ") pos ;
retStr=retStr.substring(pos);
return(retStr);
}
//清除右边空格
function js_rtrim(deststr)
{
if(deststr==null)return "";
var retStr=new String(deststr);
var pos=retStr.length;
if (pos==0) return retStr;
while (pos && retStr.substring(pos-1,pos)==" " ) pos--;
retStr=retStr.substring(0,pos);
return(retStr);
}
//清除左边和右边空格
function js_trim(deststr)
{
if(deststr==null)return "";
var retStr=new String(deststr);
var pos=retStr.length;
if (pos==0) return retStr;
retStr=js_ltrim(retStr);
retStr=js_rtrim(retStr);
return retStr;
}
//格式化输入的日期串,输入的如:"2003-9-12" 输出:"2003-09-12"
function formatDateStr(inDate){
if (inDate==null||inDate=="") return "";
var beginDate = inDate.split("-");
var mYear=beginDate[0];
var mMonth=beginDate[1];
var mDay=beginDate[2];
mMonth=((mMonth.length==1)?("0" mMonth):mMonth);
mDay=((mDay.length==1)?("0" mDay):mDay);
return mYear "-" mMonth "-" mDay;
}
//Added by wanghui 20031020 检查URL地址的端口的合法性,必须为小于65535的数字
function checkPort(inValue1,inValue2){
//先检查第一个参数的合法性,如果第二个参数是null,则第一个参数表示短信业务的‘业务处理地址'
if(inValue1 != null && inValue1.value != ""){
var array1 = inValue1.value.split(":");
if(array1.length >=4){ //如果以':'分割数组长度为四,则有两个':',所以不合法。
if(inValue2==null) alert("Business processing address port format error");
else alert("Business access address port format error");
inValue1.focus();
return false;
}
if(array1.length >= 3) {
port1 = array1[2];
if ((pos1 = port1.indexOf("/")) > = 0){ //Check whether there is a '/' after the port. If there is, the port before this symbol is the port
port1 = port1.substring(0,pos1)
}
if ( port1 == "" || checkIntNum(port1) == 0 || port1>65535){
if(inValue2 == null) alert("Business processing address port must be a number and cannot be greater than 65535");
else alert ("The business access address port must be a number and cannot be greater than 65535");
inValue1.focus();
return false;
}
}
}
//Check again Legality of the second parameter
if(inValue2 != null && inValue2.value != ""){
var array2 = inValue2.value.split(":");
if(array2. length >=4){
alert("Business processing address port format error");
inValue2.focus();
return false;
}
if(array2.length > ;= 3) {
port2 = array2[2];
if ((pos2 = port2.indexOf("/")) >= 0){
port2 = port2.substring(0,pos2 )
}
if ( port2 == "" || checkIntNum(port2) == 0 || port2>65535){
alert("Business processing address port must be a number and cannot be greater than 65535") ;
inValue2.focus();
return false;
}
}
}
return true;
}
//obj: data object
//dispStr: Failure prompt content display string
function checkUrlValid( obj, dispStr)
{
if(obj == null)
{
alert("The incoming object is empty" );
return false;
}
var str = obj.value;
var urlpatern0 = /^https?://. $/i;
if(!urlpatern0.test( str))
{
alert(dispStr "Illegal: must start with 'http://' or 'https://'!");
obj.focus();
return false;
}
var urlpatern2= /^https?://(([a-zA-Z0-9_-]) (.)?)*(:d )?. $/i;
if(!urlpatern2.test(str))
{
alert(dispStr "The port number must be a number and should be between 1-65535!");
obj.focus();
return false;
}
var urlpatern1 =/^https?://(([a-zA-Z0-9_-]) (.)?)*(:d )?(/((. )?(?)?=?&?[a-zA-Z0-9_-](?)?)*)*$/i;
if(!urlpatern1.test(str))
{
alert(dispStr "Illegal, please check!");
obj.focus();
return false;
}
var s = "0";
var t = 0;
var re = new RegExp(":\d ","ig");
while((arr = re.exec(str))!=null)
{
s = str.substring(RegExp.index 1,RegExp.lastIndex);
if(s.substring(0,1)=="0")
{
alert(dispStr "The port number cannot start with 0 !");
obj.focus();
return false;
}
t = parseInt(s);
if(t<1 || t >65535)
{
alert(dispStr "The port number must be a number and should be between 1-65535!");
obj.focus();
return false;
}
}
return true;
}
//Function name: checkVisibleEnglishChr
//Function introduction: Check whether English characters can be displayed (!"#$%&'()*,-./0123456789: ;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~)
//Parameter description: String to be checked
//Return value: true|false
//add by renhj 2004.01.05
function checkVisibleEnglishChr(strTemp)
{
var i;
for(i=0;i{
if (( strTemp.charCodeAt(i)<32) || (strTemp.charCodeAt(i)>126))
return false;
}
return true;
}
//Function Name: checkInputChr
//Function introduction: Check whether it contains non-Input characters
//Parameter description: The string to be checked
//Return value: false: Contains true: All characters can be input
//add by renhj 2004.01.05
function checkInputChr(str)
{
var notinput = ""'<>";
var i;
for (i = 0 ; notinput != null && i < notinput.length; i ) {
if (str.indexOf(notinput.charAt(i)) >= 0) {//If any
return false;
}
}
return true;
}
//Function name: checkTelExt
//Function introduction: Check whether it is a phone number and there is no extension
//Parameter description : The string to be checked
//Return value: 1 is legal, 0 is illegal
function checkTelExt(tel)
{
var i,count,isNumber;
count = 0; //There are several consecutive number strings
isNumber = 0; //Not a number
for (i=0;i{
//Judge the current Is number
if(checkIntNum(tel.charAt(i)) == 1)
{
if(isNumber == 0)
{
count = count 1;
}
isNumber = 1;
}else{
isNumber = 0;
}
if (count > 2)
{
//Indicates that some characters are illegal or have Extension number
return 0;
}
}
if((checkIntNum(tel.charAt(0)) == 1)&&
(checkIntNum(tel.charAt(tel.length- 1)) == 1)){
//It means it is legal
return 1;
}else{
//It means there are characters that are illegal
return 0;
}
}
//Function name: checkFormdata
//Function introduction: Check Form object
//Parameter description:
//obj: object to be checked,
//name: to be checked The Chinese name of the object,
//length: the length of the object being checked (<0 is not checked),
//notnull: if it is true, it is checked to be non-null,
//notSpecChar: if it is true, it is checked Are there any special characters?
//notChinessChar: If true, check whether there are Chinese characters.
//numOrLetter: If true, the check can only be numbers or English letters.
//pNumber: If true, then The check can only be positive integers,
//Return value: false: The check fails true: All characters can be input
//add by renhj 2004.03.19
//@CheckItem@ BUG:1641: 718-Renhj-20040902-Add5 Modify check digit information
function checkFormdata(obj,name,length,notnull,notSpecChar,notChinessChar,numOrLetter,pNumber){
//Check object
if (!obj ) {alert("The target is not an object, processing failed!"); return false;}
var msg;
var ilen;
//Detect Chinese characters
if (notChinessChar&&(checkChinese(obj.value ) != 1)){
msg=name "Cannot contain Chinese characters!";
alert(msg);
obj.focus();
return false;
}
//Detect special characters
if(notSpecChar&&(!checkInputChr(obj.value))){
var notinput = ""'<>";
msg=name "Illegal characters (" notinput ")! ";
alert(msg);
obj.focus();
return false;
}
//Check length
if(length>=0&&(checkLength(obj. value)>length)){
ilen=length/2;
if(pNumber){
msg=name "cannot exceed " length "numbers! ";
}else if(notChinessChar){
msg=name "cannot exceed " length " English words! ";
}else{
msg=name "cannot exceed " length " English or " ilen " Chinese characters! ";
}
alert(msg);
obj.focus();
return false;
}
//Detect non-null
if(notnull&&obj.value= =""){
msg="Please enter " name "! ";
alert(msg);
obj.focus();
return false;
}
//The detection can only be numbers or English letters
re = /[W_ ]/;
if (numOrLetter&&re.exec(obj.value)) {
msg=name "can only be numbers or English letters! ";
alert(msg);
obj.focus();
return false;
}
//Detection can only be positive integers
re = /[ D_]/;
if (pNumber&&re.exec(obj.value)) {
msg=name "can only be positive integers! ";
alert(msg);
obj.focus();
return false;
}
return true;
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn