Summary: some nice JS functions that are frequently used

Source: Internet
Author: User

Make the following Javascript file and call it on any page.
/* = =
* 1. var_to_obj --------- convert a variable to an object
* 2. is_greater --------- determine whether the number is greater than a certain number
* 3. is_less ---------- determine whether it is smaller than a certain number
* 4. compare_date ------ compare the sizes of two dates. num1> num2 return: true; num1 <= num2 return: false
* 5. is_numeric ------ determine if it is a number
* 6. is_price ----- determine whether it is in the price format: 00.00 with two decimal places
* 7. is_null ----- determines whether it is null. True: NULL. False: not empty.
* 8. isspace ------ determines whether it is a space.
* 9. isspace ---- determines whether it is a space.
* 10. is_int (---- judge whether the integer is true: Yes integer, false: Not integer
* 11. is_date ---- determine whether it is a date
* 12. is_date2 --- determine whether it is a date
* 13. isselected ---- determine whether valid data is selected
* 14. ischaracter ---- check whether it is a character
* 15. isothernamecharacter ----- check whether there are other characters that can be named
* 16. isnamecharacter ----- check whether the character can be named
* 17. isemail ----- check whether it is email
* 18. iszip ---- check for zip code
* 19. isdigitalstring ----- check whether it is a numeric string
* 20. isempty ----- The isempty function checks whether a string is null.
* 21. Trim ----- the trim function removes spaces on both sides of a string.
* 22. isdigital-The isdigital function determines whether a string is composed of digits (Int or long ).
* 23. isfloat ---- The isfloat function determines whether a string is composed of digits (Int or long or float ).
* 24. istelephone --- determines whether a string is composed of digits, '-', '*', and '()'.
* 25. datetransfer ----- date format conversion 2/18/2000 ---- 2000-2-18
* 26. transferdate ---- conversion date 2000-10-20 ----> 10/20/2000
* 27. ispassword ----- check whether the password is used
* 28. isleapyear ----- determines whether it is a function of the runyear.
* 29. getdaysinmonth ---- function for obtaining the number of days per month
* 30. jtrimstr --- Remove all spaces in the string
* ===================================================== ======= */

Function doinputconnotempty (txtinput, minlength, salert ){
Txtinput. value = trim (txtinput. value );
VaR m_isvalid = true;
If (isempty (txtinput. Value) m_isvalid = false;
If (minlength> 0 ){
If (txtinput. value. Length <minlength) m_isvalid = false;
}
If (! M_isvalid ){
If (salert! = "") Window. Alert (salert );
Txtinput. Focus ();
}
Return m_isvalid;
}

// convert a variable to an object
function var_to_obj (VAL)
{< br> This. value = val;
}< br> // determines whether the value is greater than a certain number.
function is_greater (field, crit, limit)
{< br> var ret = (is_numeric (field,-1 ))? (Field. value> limit): false;
If (! RET)
docritcode (field, crit, "value must be greater than" + limit);
return (RET );
}< br> // determines whether the value is smaller than a certain number.
function is_less (field, crit, limit)
{< br> var ret = (is_numeric (field,-1 ))? (Field. value If (! RET)
docritcode (field, crit, "value must be less than" + limit);
return (RET);
}

function is_numeric (field, crit, MSG)
{< br> var ret = true;
var numstr = "0123456789 ";
var decused = false;
var CHR;
for (I = 0; I {< br> CHR = field. value. charat (I);
If (numstr. indexof (CHR, 0) =-1)
{< br> If ((! Decused) & CHR = ". ")
{< br> decused = true;
}< br> else
{< br> ret = false;
}< BR >}< br> If (! RET)
docritcode (field, crit, MSG);
return (RET );
}< br> // determine whether it is a price
function is_price (field, crit, MSG)
{< br> var ret = true;
var numstr = "0123456789";
var decused = false;
var CHR;
for (I = 0; I {< br> CHR = field. value. charat (I);
If (numstr. indexof (CHR, 0) =-1)
{< br> If ((! Decused) & CHR = ". ")
{< br> decused = true;
}< br> else
{< br> ret = false;
}< BR >}< br> If (RET)
{< br> If (decused & (field. value. length-field.value.indexOf ('. ') <4)
;< br> else if (decused)
ret = false;
}< br> If (! RET)
docritcode (field, crit, MSG);
return (RET);
}

// determine whether it is null
function is_null (field, crit, MSG)
{< br> var text = "" + trim (field. value);
If (text. length)
{for (VAR I = 0; I If (text. charat (I )! = "" & Text. charat (I )! = "")
break;
if (I> = text. length) {
ret = true;}
else {
ret = false;}
}< br> else
ret = true;
If (RET)
docritcode (field, crit, MSG);
return (RET);
}< br> function isspace (field)
{< br> var text = "" + field. value;
If (text. length)
{< br> for (VAR I = 0; I If (text. charat (I )! = "" & Text. charat (I )! = "")
break;
if (I> = text. length)
field. value = "";
}< BR >}

function docritcode (field, crit, MSG)
{< br> If (-1! = Crit)
{< br> alert (MSG)
If (CRIT = 1)
{< br> field. focus (); // focus does not work on certain Netscape versions
field. select ();
}< BR >}< br> // determines whether it is an integer
function is_int (field, crit, MSG) {
var ret = true;
var numstr = "0123456789";
var CHR;
If (field. value. length = 0)
{< br> ret = false;
}< br> for (I = 0; I {< br> CHR = field. value. charat (I);
If (numst R. indexof (CHR, 0) =-1)
{< br> ret = false;
}< BR >}< br> If (! RET)
docritcode (field, crit, MSG);
return (RET );
}< br> // determines whether it is a date
function is_date (field, crit, MSG) {
var ret = false;
var mark1;
var mark2;
var days;
var y;
var m;
var D;
If (field. value = "")
return true;
Cd = new date ();

If (mark1 = field. value. indexof ('-') =-1)
Mark1 = field. value. indexof ('-')
If (mark1>-1)
{
If (mark2 = field. value. indexof ('-', mark1 + 1) =-1)
Mark2 = field. value. indexof ('-', mark1 + 1 );
If (mark2>-1) & (mark2 + 1 <field. value. Length ))
{
Y = parseint (field. value. substring (0, mark1), 10 );
M = parseint (field. value. substring (mark1 + 1, mark2), 10 );
D = parseint (field. value. substring (mark2 + 1, field. value. length), 10 );

Year = new var_to_obj (y );
Month = new var_to_obj (m );
Day = new var_to_obj (d );
Days = getdaysinmonth (month. Value, year. Value) + 1;

If (
(Is_greater (day,-1, 0) & (is_less (day,-1, days ))&&
(Is_greater (month,-) & (is_less (month ))&&
(Is_greater (year,-) & (is_less (year ))
)
Ret = true;
}
}
If (! RET) docritcode (field, crit, MSG );

Return (RET );
}

Function docrit (field, crit, MSG)
{
If (-1! = Crit ))
{
Alert (MSG );
If (CRIT = 1)
{
Field. Focus (); // focus does not work on certain Netscape versions
}
}
}
// Determine whether valid data is selected
Function isselected (field, crit, MSG)
{
Value = "" + field. Options [field. selectedindex]. value;
If (value = "0 ")
Ret = false;
Else
Ret = true;
If (! RET)
Docrit (field, crit, MSG );
Return (RET );
}

// Check whether it is a character
// Ccharacter: input value
Function ischaracter (ccharacter)
{
VaR sformat = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz ";

If (sformat. indexof (ccharacter, 0) =-1)
{
Return false;
}

Return true;
}

// Determine whether it is a combination of numbers and letters
Function ischrandnum (ccharacter)
{
For (ilen = 0; ilen <Str. length; ilen ++)
{
If (Str. charat (ilen) <'0' | Str. charat (ilen)> '9 ')
{
If (Str. charat (ilen) <'A' | Str. charat (ilen)> 'Z ')
{
If (Str. charat (ilen) <'A' | Str. charat (ilen)> 'Z ')
Return false;
}
}
}
Return true;
}

// Check whether there are other characters that can be named
// Ccharacter: input value
Function isothernamecharacter (ccharacter)
{
VaR sformat = "_";

If (sformat. indexof (ccharacter, 0) =-1)
{
Return false;
}

Return true;
}
Function isothernamecharacter1 (ccharacter)
{
VaR sformat = "-";

If (sformat. indexof (ccharacter, 0) =-1)
{
Return false;
}

Return true;
}

Function isothernamecharacter2 (ccharacter)
{
VaR sformat = ".";

If (sformat. indexof (ccharacter, 0) =-1)
{
Return false;
}

Return true;
}

// Check whether the name can be a character
// Svalue: input value
Function isnamecharacter (svalue)
{
If (svalue = NULL)
{
Return false;
}

For (I = 0; I <svalue. length; I ++)
{
VaR ccharacter = svalue. charat (I );
If (isdigital (ccharacter) = false & ischaracter (ccharacter) = false & isothernamecharacter (ccharacter) = false & isothernamecharacter1 (ccharacter) = false & isothernamecharacter2 (ccharacter) = false)
{
Return false;
}
}

Return true;
}
// Check whether the password is used
Function ispassword (svalue)
{
If (svalue = NULL)
{
Return false;
}

For (I = 0; I <svalue. length; I ++)
{
VaR ccharacter = svalue. charat (I );
If (isdigital (ccharacter) = false & ischaracter (ccharacter) = false & isothernamecharacter (ccharacter) = false)
{
Return false;
}
}

Return true;
}

// Check whether it is email
// Svalue: input value. Valid format: a@ B .c.d.
Function isemail (svalue)
{
VaR ifirstindex = 0;
VaR isecondindex = svalue. indexof ('@');
If (isecondindex =-1)
{
Return false;
}

VaR stemp = svalue. substring (ifirstindex, isecondindex );
If (isnamecharacter (stemp) = false)
{
Return false;
}

Isecondindex = svalue. indexof ('.');
If (isecondindex =-1 | svalue. substring (svalue. Length-1, svalue. Length) = '.')
{
Return false;
}
Else if (stemp. Length = svalue. Length-2) // The last two characters are '@' and '.'
{
Return false;
}
Else
{
VaR stempvalue = svalue;
Isecondindex = svalue. indexof ('@');
While (isecondindex! =-1)
{
Ifirstindex = isecondindex + 1;
Stempvalue = stempvalue. substring (ifirstindex, stempvalue. Length); // The right section of Value
Isecondindex = stempvalue. indexof ('.');
// Document. Write ("stempvalue =" + stempvalue + "<br> ");
Stemp = stempvalue. substring (0, isecondindex );
// Document. Write ("stemp =" + stemp + "<br> ");
If (isnamecharacter (stemp) = false)
{
Return false;
}
}

If (isnamecharacter (stempvalue) = false)
{
Return false;
}
}

Return true;
}

// Check for zip code
// Svalue: the input value. The valid format is a six-digit integer.
Function iszip (svalue)
{
If (svalue = NULL)
{
Return false;
}

If (svalue. length! = 6)
{
Return false;
}
Else
{
For (I = 0; I <6; I ++)
{
If (isdigital (svalue. charat (I) = false)
{
Return false;
}
}
}

Return true;
}

// Check whether the string is a numeric string
// Svalue: input value
Function isdigitalstring (svalue)
{
If (svalue = NULL)
{
Return false;
}

For (I = 0; I <svalue. length; I ++)
{
If (isdigital (svalue. charat (I) = false)
{
Return false;
}
}
}

// The isempty function checks whether a string is null.
Function isempty (HIS)
{
Flag = true;
For (VAR I = 0; I {
If (his. charat (I )! = "")
{
Flag = false;
Break;
}
}
Return flag;
}
// Remove spaces on both sides of a string from the trim Function
Function trim (HIS)
{
// Locate the start position of the string
Pos_start =-1;
For (VAR I = 0; I {
If (his. charat (I )! = "")
{
Pos_start = I;
Break;
}
}
// Locate the end position of the string
Pos_end =-1;
For (VAR I = His. Length-1; I> = 0; I --)
{
If (his. charat (I )! = "")
{
Pos_end = I;
Break;
}
}
// Return string
Str_return = ""
If (pos_start! =-1 & pos_end! =-1)
{
For (VAR I = pos_start; I <= pos_end; I ++)
{
Str_return = str_return + his. charat (I );
}
}
Return str_return;
}
// The isdigital function determines whether a string is composed of digits (Int or long ).
Function isdigital (STR)
{
For (ilen = 0; ilen <Str. length; ilen ++)
{
If (Str. charat (ilen) <'0' | Str. charat (ilen)> '9 ')
{
Return false;
}
}
Return true;
}
// The isfloat function determines whether a string is composed of digits (Int or long or float ).
Function isfloat (STR)
{
Flag_dec = 0
For (ilen = 0; ilen <Str. length; ilen ++)
{
If (Str. charat (ilen) = '.')
{
Flag_dec ++;
If (flag_dec> 1)
Return false;
Else
Continue;
}
If (Str. charat (ilen) <'0' | Str. charat (ilen)> '9 ')
{
Return false;
}
}
Return true;
}
// The istelephone function determines whether a string is composed of numbers, '-', and '*'.
Function istelephone (STR)
{
For (ilen = 0; ilen <Str. length; ilen ++)
{
If (Str. charat (ilen) <'0' | Str. charat (ilen)> '9 ')
{
If (Str. charat (ilen )! = '-') & (Str. charat (ilen )! = '*'))
Return false;
}
}
Return true;
}

// Compare the values of two dates, num1> num2 return: true; num1 <= num2 return: false
Function compare_date (num1, num2)
{
VaR pos1, pos2, end;
VaR para1, para2, para3, para4, para5, para6;

// para1: year
// para2: month
// para3: day
end = num1.length;
pos1 = num1.indexof ("-", 0);
pos2 = num1.indexof ("-", pos1 + 1);
para1 = num1.substring (0, pos1);
para2 = num1.substring (pos1 + 1, pos2);
para3 = num1.substring (pos2 + 1, end);
para1 = parseint (para1, 10);
para2 = parseint (para2, 10);
para3 = parseint (para3, 10);
end = num2.length;
pos1 = num2.indexof ("-", 0);
pos2 = num2.indexof ("-", pos1 + 1);
para4 = num2.substring (0, pos1);
para5 = num2.substring (pos1 + 1, pos2);
para6 = num2.substring (pos2 + 1, end);
para4 = parseint (para4, 10);
para5 = parseint (para5, 10);
para6 = parseint (para6, 10);
If (para1> para4)
{< br> return true;
}< br> else if (para1 = para4)
{< br> If (para2> para5)
{< br> return true;
}< br> else if (para2 = para5)
{< br> If (para3> para6)
{< br> return true;
}< BR >}l. co
return false;
}

// remove all spaces in the string
function jtrimstr (STR)
{< br> var I = 0;
var J;
var Len = Str. length;
trimstr = "";
while (I {< br> If (Str. charat (I )! = "")
{< br> trimstr = trimstr + Str. charat (I);
}< br> I ++;
}< br> return (trimstr );
}< br> // conversion date
function transferdate (STR)
{< br> var M = 4;
var strlen = Str. length
var n = strlen-1;
while (n> = strlen-2)
{
If (Str. charat (n) = "-")
{< br> break;
}< br> N = n-1
}< br> trimstr = Str. substring (m + 1, n) + "/" + Str. substring (n + 1, strlen) + "/" + Str. substring (0, m)
return (trimstr)
}

// 1. Obtain the function of the number of days per month
// Parameter description: month-month; year-year
// Return value: days -- days
Function getdaysinmonth (month, year ){
VaR days;
If (month = 1 | month = 3 | month = 5 | month = 7 | month = 8 | month = 10 | month = = 12) days = 31;
Else if (month = 4 | month = 6 | month = 9 | month = 11) days = 30;
Else if (month = 2 ){
If (isleapyear (year) {days = 29 ;}
Else {days = 28 ;}
}
Return (days );
}
// 2. Determine whether the function is a runyear Function
// Parameter description: year -- year
// Return value: True is returned if it is a runyear; otherwise, false is returned.

Function isleapyear (year ){
If (Year % 4) = 0) & (Year % 100 )! = 0) | (Year % 400) = 0 )){
Return (true );
} Else {return (false );}
}

Function ischarnum (STR)
{
For (ilen = 0; ilen <Str. length; ilen ++)
{
If (Str. charat (ilen) <'0' | Str. charat (ilen)> '9 ')
{
If (Str. charat (ilen )! = 'X '))
Return false;
}
}
Return true;
}

Function ischarsf (STR)
{
For (ilen = 0; ilen <Str. length; ilen ++)
{
If (Str. charat (ilen) <'0' | Str. charat (ilen)> '9 ')
{
If (Str. charat (ilen) <'A' | Str. charat (ilen)> 'Z ')
Return false;
}
}
Return true;
}

// Determine whether a string is composed of numbers and "-"

Function ismonth (STR)
{
For (ilen = 0; ilen <Str. length; ilen ++)
{
If (Str. charat (ilen) <'0' | Str. charat (ilen)> '9 ')
{
If (Str. charat (ilen )! = '-'))
Return false;
}
}
Return true;
}

// Dialog box
Function popmodaldialog (URL, argS, height, width ){
Return window. showmodaldialog (URL, argS, "dialogheight:" + height + "PX; dialogwidth:" + width + "PX; center: Yes; help: No; resizable: No; Status: no ;");
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.