First, the demand
Test the input year for leap years and to prevent illegal input
Second, demand analysis
1. Display an input box for input and submit a button for input;
2. Filter illegal input, when and only if the input character Embox is a number and the number is not 0 (note: ad 00 does not exist) to do further processing, otherwise prompt input error;
Write with Html&javascript only:
function Checknum (value) {//value The value passed in as input box var Reg =/^[0-9]*$/; if return true ; Enter the correct returnfalse; Illegal input}
3. Determine whether the Swiss year is based on the number entered:
A. The average year can be divisible by 4 and not divisible by 100 for leap years. (such as 2004 years is a leap year, 1900 is not a leap year) b. The century can be divisible by 400 is leap years. (as 2000 is a leap year, 1900 is not a leap year)
if((document.getElementById ("input"). value%4==0&&document.getelementbyid ("input"). value% -!=0)|| document.getElementById ("input"). value% -==0) document.getElementById ("Output"). innerhtml="enter years as leap year.";Elsedocument.getElementById ("Output"). innerhtml="Enter the year as common year.";
Third, test cases
Number |
Input |
Expected output |
1 |
2008 |
Enter years as leap year. |
2 |
2000 |
Enter years as leap year. |
3 |
1900 |
Enter the year as common year. |
4 |
Abcd |
Please check that the input is correct. |
5 |
0 |
Please check that the input is correct. |
Iv. Practical Testing
Five, Source: Html&javascript
<!DOCTYPE HTML><HTML><HeadLang= "en"><MetaCharSet= "GB2312"><style>Body{text-align:Center}</style><Body>Please enter the year:<inputtype= "text"ID= "Input"Autofocus= "Autofocus"/><inputtype= "Submit"value= "OK"onclick= "Test ()"/><PID= "Output"></P><Script>functionTest () {if(!Checknum (document.getElementById ("input"). Value)||document.getElementById ("input"). Value==0) document.getElementById ("Output"). InnerHTML="Please check that the input is correct."; Else if( (document.getElementById ("input"). Value%4==0&&document.getElementById ("input"). Value% -!=0)||document.getElementById ("input"). Value% -==0) document.getElementById ("Output"). InnerHTML="enter years as leap year."; Elsedocument.getElementById ("Output"). InnerHTML="Enter the year as common year.";}functionChecknum (value) {varReg= /^[0-9]*$/; if(Reg.test (value))return true; return false;}</Script></Body></HTML>
Leap year judgment of software test instance