Home Web Front-end JS Tutorial Some commonly used Javascript judgment functions_Basic knowledge

Some commonly used Javascript judgment functions_Basic knowledge

May 16, 2016 pm 05:51 PM
javascript Judgment function

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript and WebSocket: Building an efficient real-time image processing system JavaScript and WebSocket: Building an efficient real-time image processing system Dec 17, 2023 am 08:41 AM

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data

See all articles