Practical JavaScript transfer value, data validation, event triggering summary

Source: Internet
Author: User
Tags date format datetime html page regular expression return trim window
javascript| data

Practical JavaScript transfer value, data validation, event triggering summary

  1. And JSP transfer value problem:


1 Data validation Transfer value of normal submission form

HTML page Input:

<form name = "Form1" >

<input.....>

</form>


Javascript gets the value and processes it:

<script language= "JavaScript" >

function CheckForm1 (Text1)

{

if (text1.name.value== "")

{alert ("null");

return false;

}

return true;

}

</script>

This is equivalent to passing a function of a pointer past to JavaScript, which points to all the elements in the form. When quoting, you don't have to write form1.name.value instead of Text1.name.value. Of course, if you have to write that, You don't have to pass the this pointer. I am inclined to pass the use of this pointer.


2 Value between window and window

HTML page Links:


<a href= "Javascript:openwin (<%=rs.getstring (" article_id ")%>)" title= "<%=rs.getstring (" title ")%>"/ >


<script>

function Openwin (ID) {

window.open ("articleshow.jsp?id=" +id, "", "height=450,width=550,resizable=yes,scrollbars=yes,status=no,toolbar= No,menubar=no,location=no ");

}

</script>

The value ID passed and opens a new window


 2. Data validation Issues


1) Verify null value

if (from.textname.value== "") {alert ("must be entered");

Note that the double equals, with the Java syntax


2) Verify the number

if (isNaN (CheckText1.purchase_quantity.value))

{

Alert ("Please input the number of purchase numbers!");

CheckText1.purchase_quantity.focus ();

return false;

}


3) Verification Date

Note that it is best to use the onchange event on the date input control, and of course the better way is to use JavaScript to write the date input control to solve, but when multiple data is affected by the speed of this case is not recommended.

HTML input:

<td><input type= "text" name= "confirm_date" value= ""/></td>


Whether the timing is correct

function IsDate (checktext) {

var datetime;

var year,month,day;

var gone,gtwo;

if (Trim (checktext.value)!= "") {

Datetime=trim (Checktext.value);

if (datetime.length==10) {

Year=datetime.substring (0,4);

if (isNaN (year) ==true) {

Alert ("Please enter the date!" Format (YYYY-MM-DD) (2008-01-01)! ");

Checktext.focus ();

return false;

}

Gone=datetime.substring (4,5);

Month=datetime.substring (5,7);

if (isNaN (month) ==true) {

Alert ("Please enter the date!" Format (YYYY-MM-DD) (2008-01-01)! ");

Checktext.focus ();

return false;

}

Gtwo=datetime.substring (7,8);

Day=datetime.substring (8,10);

if (isNaN (day) ==true) {

Alert ("Please enter the date!" Format (YYYY-MM-DD) (2008-01-01)! ");

Checktext.focus ();

return false;

}

if ((gone== "-") && (gtwo== "-")) {

if (month<1| | MONTH>12) {

Alert ("Month must be between 01 and 12!");

Checktext.focus ();

return false;

}

if (day<1| | DAY>31) {

Alert ("Date must be between 01 and 31!");

Checktext.focus ();

return false;

}else{

if (month==2) {

if (Isleapyear (year) &&day>29) {

Alert ("February date must be between 01 and 29!");

Checktext.focus ();

return false;

}

if (!isleapyear (year) &&day>28) {

Alert ("February date must be between 01 and 28!");

Checktext.focus ();

return false;

}

}

if (month==4| | month==6| | month==9| | month==11) && (day>30)) {

Alert ("At four, six, nine, November \ Date must be between 01 and 30!");

Checktext.focus ();

return false;

}

}

}else{

Alert ("Please enter the date!" Format (YYYY-MM-DD) (2008-01-01));

Checktext.focus ();

return false;

}

}else{

Alert ("Please enter the date!" Format (YYYY-MM-DD) (2008-01-01));

Checktext.focus ();

return false;

}

}else{

return true;

}

return true;

}

In this case, the normal user input will be validated more useful.

4) Verifying letters, etc.

if (document.f1.dep.value!= ' AB ')

NULL is NULL to judge num Digit AB letter chs Chinese character bit number

{

Alert ("Sorry, please enter the name of the door is called" Hans!)

Document.f1.dep.focus ()

return False

}

}

But this method doesn't seem to be working.

5 in a certain format to verify: The best is the regular expression this is the best use, but also to verify the complexity of the format required.

  3. Event triggering

MSDN Home > MSDN Library > Web Development > HTML and CSS > HTML and DHTML Reference > Events DHTML All Events

Note that the above path needs to be accessible via the English version of MSDN!

Finally, attach an example of the background color change in the selected (mouse click line):

<div align= "center" >

<center>

<table border= "1" width= "cellspacing=" 0 "cellpadding=" 0 ">

<tr onmouseout = "this.style.background="; This.style.bordercolor= ' ">

<TD width= "> </td>"

</tr>

<tr onmouseout = "this.style.background="; This.style.bordercolor= ' ">

<TD width= "> </td>"

</tr>

</table>

</center>

</div>

The main application is Onmouseove, onmouseout event This can be found in the MSDN link above, we use JavaScript to verify the value of the time, used the onsubmit event, and the validation date with the onchange event



Related Article

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.