Software Testing-4 procedures for judging leap years and preventing entry of illegal characters

Source: Internet
Author: User

First, the topic

Determine if the year entered is a leap years

Second, the implementation of the program

I continue to use javascript+html to achieve:

The procedure for judging leap years is as follows, regardless of the input of the exception:

function isleapyear (y) {    return (y% 400 = = 0) | | (y% 4 = = 0 && y% = 0 );}

But in practice, you have to consider whether there is an exception input, so I can check the input before calling the function to ensure that the input is valid:

function Isint (input) {    var reg =/^[0-9]+$/;     if (Reg.test (input))         return true ;     return false ;}

Use the Isint () function to ensure that the input is valid before each judgment:

function Check () {    var input = document.getElementById ("year"). Value;     if (Isint (input)) {        if(isleapyear (input)) alert ("is a leap year");         Else alert ("Not a leap year");    }     Else alert ("The input is illegal! ");}

Click here to access the program:http://zhaobi.org/softwaretest/Test3.html

Third, testing

Test Case:

Input Expected output
2400 Yes
1900 No
2004 Yes
2015 No
Adc Exception
Null input Exception

Test:

Iv. Source Code

<!DOCTYPE HTML><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><title>EditBox</title></Head><Body><Divstyle= "width:220px">Please enter the year:<inputtype= "text"ID= "Year"/><BR><inputtype= "button"value= "OK"OnClick= "Check ()"/><Scripttype= "Text/javascript">functionIsint (input) {varReg= /^[0-9]+$/; if(Reg.test (input))return true; return false;}functionisleapyear (y) {return(y%  - == 0 ) ||(y% 4 == 0 &&y%  - != 0 );}functionCheck () {varinput=document.getElementById (" Year"). Value; if(Isint (input)) {if(Isleapyear (input)) alert ("is a leap year"); ElseAlert ("not a leap year"); }    ElseAlert ("The input is illegal! ");}</Script></Div></Body></HTML>

V. Summary

In fact, you can also merge the instrumented input snippet into the Isleapyear () function, which reduces the number of functions.

Software Testing-4 procedures for judging leap years and preventing entry of illegal characters

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.