我的html代码在
之下 <script src="js/validate.js"></script>
<label>FIRST NAME:</label>
</td>
<td>
<input type="text" class="firstname" id="firstname" onKeyUp="firstname()" />
</td>
<td>
<label id="fn"></label>
我的js代码在
之下function firstname()
{
var nname=document.getElementById("firstname").value;
var l=nname.length > 3 ;
if( !l)
{
producePrompt("firstname should not be less than 4 characters","fn","red");
return false;
}
else
if(!nname.match(/^[a-zA-Z\s]*$/))
{
producePrompt("incorrect name","fn","red");
return false;
}
else
{
producePrompt("correct","fn","green");
return true;
}
}
function producePrompt(message,promptlocation,color)
{
document.getElementById(promptlocation).innerHTML= message;
document.getElementById(promptlocation).style.color= color;
}
但是我的代码没有显示消息或没有正确验证它 js位于名为validate.js的文件下,而html位于index.html中 我尝试了两种类型(,)来包含js文件,但没有进行验证
答案 0 :(得分:0)
试试这个,对我来说很好(验证工作):
js:
function checkinput()
{
var nname=document.getElementById("firstname").value;
var l=nname.length > 3 ;
if(!l)
{
console.log("firstname should not be less than 4 characters","fn","red");
return false;
}
else
if(!nname.match(/^[a-zA-Z\s]*$/))
{
console.log("incorrect name","fn","red");
return false;
}
else
{
console.log("correct","fn","green");
return true;
}
}
HTML:
<form>
<label>FIRST NAME:</label> <input type="text" class="firstname" id="firstname" onblur="checkinput()" />
</form>
答案 1 :(得分:0)
<body>
// all your html contents
// load the scripts here
<script src="js/validate.js"></script>
</body>
OR
<body>
// all your html contents
<script>
// javascript contents
</script>
</body>