HTML, JavaScript Basics (iv)

Source: Internet
Author: User
Tags date1 html form

1. Judge that the second date is larger than the first date

How to use the script to determine the user input string is the following time format 2004-11-21 must ensure that the user input is this format, and is the time, such as the month is not greater than 12 and so on, in addition I need the user to enter two, and the latter one later than the previous night, only allowed to use JavaScript, Please give me a detailed answer,

You can use regular expressions to judge the format in advance and then press to extract the contents of each time field.

<scripttype= "Text/javascript" >

Window.onload = function ()

{

This is written in order to achieve the separation of JS code and HTML code, when I modify JS, can not affect the HTML code.

document.getElementById ("Frm1"). onsubmit=

function () {

Vard1 = This.d1.value;

Vard2 = This.d2.value;

if (!verifydate (D1)) {alert ("The first date is not formatted"); return false;}

if (!verifydate (D2)) {alert ("The second date is not formatted"); return false;}

if (!comparedate (D1,D2)) {alert ("Second date is smaller than first date"); return false;}

};

}

Functioncomparedate (D1,D2)

{

var arrayD1 = D1.split ("-");

var date1 = newdate (arrayd1[0],arrayd1[1],arrayd1[2]);

var arrayD2 = D2.split ("-");

var date2 = newdate (arrayd2[0],arrayd2[1],arrayd2[2]);

if (Date1 > Date2) return false;

return true;

}

Functionverifydate (d)

{

var datepattern =/^\d{4}-(0?[ 1-9]|1[0-2])-(0?[ 1-9]| [1-2]\d|3[0-1]) $/;

Return Datepattern.test (d);

}

</script>

<formid= "Frm1" action= "xxx.html" >

<inputtype= "text" name= "D1"/>

<inputtype= "text" name= "D2"/>

<inputtype= "Submit"/>

</form>

2. Use table to display n records, every 3 lines to change the color, that is, three-way with red font, 4,5,6 with green font, 7,8,9 with red color font.

<body>

<tableid= "TBL" >

<tr><td>1</td></tr>

<tr><td>2</td></tr>

<tr><td>3</td></tr>

<tr><td>4</td></tr>

<tr><td>5</td></tr>

<tr><td>6</td></tr>

<tr><td>7</td></tr>

<tr><td>8</td></tr>

<tr><td>9</td></tr>

<tr><td>10</td></tr>

</table>

</body>

<scripttype= "Text/javascript" >

Window.onload=function ()

{

var tbl =document.getelementbyid ("tbl");

Rows =tbl.getelementsbytagname ("tr");

for (i=0;i<rows.length;i++)

{

Varj = parseint (I/3);

if (j%2==0) rows[i].style.backgroundcolor= "#f00";

else rows[i].style.backgroundcolor= "#0f0";

}

}

</script>

3. How do I verify that the contents of the Value text box are all digital before the HTML form is submitted? Otherwise, prompt the user and terminate the submission?

<form onsubmit= ' Returnchkform (this) ' >

<inputtype= "text" name= "D1"/>

<input type= "Submit"/>

</form>

<script type= "Text/javascript"/>

function Chkform (this)

{

Varvalue = Thist.d1.value;

Varlen = Value.length;

for (vari=0;i<len;i++)

{

if (Value.charat (i) > "9" | | Value.charat (i) < "0")

{

Alert ("contains non-numeric characters");

Returnfalse;

}

}

Returntrue;

}

</script>

4. Write a JavaScript code that verifies that all the content entered in the HTML text box is numeric

<inputtype= "text" id= "D1" onblur= "Chknumber (This)"/>

<script type= "Text/javascript"/>

Functionchknumber (Eletext)

{

Varvalue = Eletext.value;

Varlen = Value.length;

for (vari=0;i<len;i++)

{

if (Value.charat (i) > "9" | | Value.charat (i) < "0")

{

Alert ("contains non-numeric characters");

Eletext.focus ();

Break

}

}

}

</script>

In addition to writing the code, you should write the experiment steps on the page and add the implementation ideas to the code so that the interviewer can see your intentions and check your results.

5, say that you have used those Ajax technology and framework, say their differences

HTML, JavaScript Basics (iv)

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.