<html>
<head>
</head>
<body>
<script type="text/javascript" language="javascript">
function isUpperCase(str) {
return str === str.toUpperCase();
}
function validate()
{
var flag=0;
var x=document.getElementById("f").value;
var y=document.getElementById("l").value;
var w=document.getElementById("c").value;
var p=document.getElementById("o").value;
//the first name should be all capital and less than 20 characters
if(x.length>20 || x=="" || isUpperCase(x)==false)
{
flag=1;
document.getElementById("f").style.backgroundColor = "yellow";
}
//the last name should be less than 15 characters and all capital
if(y.length>15 || y=="" || isUpperCase(y)==false)
{
flag=1;
document.getElementById("l").style.backgroundColor = "yellow";
}
//the customer number length should be 5
if(w=="" || w.length!=5)
{
flag=1;
document.getElementById("c").style.backgroundColor = "yellow";
}
//order num length should be 5,first character should be 'p' and next three should be numbers.
if(p.length!=5 || p.charAt(0)!='p' || isNaN(p.substr(1,3)))
{
flag=1;
document.getElementById("o").style.backgroundColor = "yellow";
}
if(flag==1)
alert("check the colored boxes");
return flag;
}
</script>
<form name="myform" onsubmit=" return validate()" action="" method="post">
first name:<input id="f" type="text" name="first"><br>
last name:<input id="l" type="text" name="last"><br>
customer no:<input id="c" type="text" name="customer_num"><br>
order no:<input id="o" type="text" name="order_num"><br>
date:<input id="d" type="date" name="date"><br>
<input type="submit" value="submit">
</form>
</body>
</html>
私はjavascriptを初めて使用しています。 関数validateはフォーム入力の制約をチェックし、条件が満たされない場合は入力の背景色が変更され、最後にカラーボックスのチェックを求める警告が表示されます。フォームを送信すると、間違った値の入力ボックスは色が変わるはずです
のようになりますか? – Rayon
フォームがvalidated.itを取得していないのは、 – Ali
あなたのフラグが '0' /' 1'であるときにリフレッシュされます。なぜ 'boolean'を使用しないのですか? – pwolaq