做作业写了几个javascript和html,readme要怎么写,可以给个例子吗?

2025-04-08 22:05:43
推荐回答(1个)
回答1:





Javascript+HTML用正则表达式写一段输入的验证代码

table {
position: relative;
margin: auto;
font-family: Consolas;
font-size: 12px;
border: 1px solid black;
border-collapse: collapse;
width: 800px;
}

table tr td {
border: 1px solid black;
}

.center {
text-align: center;
}


var V = {
date: new Date,
isError: true,
validationType: [
                  'cannot be blank',
                    'must be a valid email',
                    'must contain only numbers,single spaces.',
                    'must contain between 13 & 18 numbers, and single spaces.',
                    'expiry date cannot be expired.'
                 ],
trim: function(v)
{
return v.replace(/^\s*|\s*$/g, '');
}, 
validateOnblur: function(obj, validationType)
{
var val = V.trim(obj.value);
switch (validationType)
            {
            case 0:
             if (V.trim(obj.value) == '')
                {
             obj.value = '';
                 return V.validationType[0];
                }
             return '';
            case 1:
             if (!/^[\w\-]+@[\w\-]+\.\w+$/.test(val))
                {
             obj.value = '';
                 return V.validationType[1];
                }
             return '';
            case 2:
             if (!/^(\+\(\d+\))?\d+$/.test(val))
                {
             obj.value = '';
                 return V.validationType[2];
                }
             return '';
            case 3:
             if (!/^[\d\s]{13,18}$/.test(val))
                {
             obj.value = '';
                 return V.validationType[3];
                }
             return '';
            case 4:
             var expirydate = document.getElementsByName('expirydate');
             var year = expirydate[0].value;
             var month = expirydate[1].value;
             if (new Date(year, parseInt(month) - 1, 0) <= V.date)
                    {
                    return V.validationType[4];
                    }
             return '';
            default:
             return '';
            }
},
checkNow: function(obj)
{
var _name = obj.name, validationType = -1;
            if (_name == 'firstname' || _name == 'lastname' || _name == 'address')
            {
             validationType = 0;
            }
            else if (_name == 'email')
            {
                validationType = 1;
            }
            else if (_name == 'phone')
            {
                validationType = 2;
            }
            else if (_name == 'credit')
            {
             validationType = 3;
            }
            else if (_name == 'expirydate')
            {
             validationType = 4;
            }
            var cell = obj.parentElement;
            if (cell.children[cell.children.length - 1].tagName.toLowerCase() == 'div')
            {
            cell.removeChild(cell.children[cell.children.length - 1]);
            V.isError = false;
            }
            var error = V.validateOnblur(obj, validationType);
            if (error != '')
            {
             var info = document.createElement('div');
            info.style.color = 'red';
            info.innerText = error;
            cell.appendChild(info);
            V.isError = true;
            }
},
displayError: function(rows, len, flag)
{
for ( var i = 0; i < len; i++)
        {
var cell = rows[i].cells[1];
if (!cell)
            {
            continue;
            }
var obj = cell.children[0];
if (flag)
                {
if (typeof obj.value != 'undefined')
                    {
        V.checkNow(obj);
                    }
                }
else
{
obj.onblur = function()
        {
         V.checkNow(this);
         }
if (obj.tagName.toLowerCase() == 'select')
                    {
cell.children[1].onblur = function()
{
         V.checkNow(this);
}
                    }
}
        }
}
};
window.onload = function ()
    {
var form = document.formValidation;
var table = form.getElementsByTagName('table')[0];
var rows = table.rows, len = rows.length;
document.getElementById('submit').onclick = function()
{
V.displayError(rows, len, true);
}

form.onsubmit = function()
{
return !V.isError;
}
V.displayError(rows, len, false);

var date = V.date;
var expirydate = document.getElementsByName('expirydate');
for ( var i = 2014; i < 2055; i++)
        {
var option = document.createElement('option');
option.value = i;
option.innerText = i;
expirydate[0].appendChild(option);
        }
expirydate[0].value = date.getFullYear();
    }















































!Form Validation!
first name:
last name:
address:
email:
phone:
delivery method:



credit card number field: 
expiry date:
 year

01
02
03
04
05
06
07
08
09
10
11
12
 month