这是我写的代码,但Javascript验证无效......语法错误......这是一篇过去的纸质问题。 该程序包含一个HTML表单和一个带有验证的Javascript函数 验证是用户名不应为空,电话文本框应为数字
Javascript功能
function val(this)
{
if(document.Regal.UName.value.length==0)
{
alert("UserName Empty");
return false;
}
if (isNaN(document.valid.TeleNO.value))
{
prompt("Telephone Empty");
}
}
</script>
</head>
<body>
<form="Regal">
<h1><b>Regal Real Estate Agency</b></h1><br>
<h2><b><u>Property Inspection Request Form</u></b></h2><br>
Name:
<input type="text" name="Name"><br>
Telephone:
<input type="text" name="Telephone"><br>
Reference Code:
<input type="text" name="RC"><br>
Select Site To Vist
<select name="Select Site">
<option value ="Select Site"checked="checked">Select Site</option>
<option value ="Colombo">Colombo</option>
<option value ="Gampha">Gampha</option>
<option value ="Galle">Galle</option>
</select>
When Is It Convient For Us To Contact You To Make An Appointment?
<input type="radio" name="appointment" value="Morning" checked="true">
<input type="radio" name="appointment" value="Afternoon">
<br>
<input type="button" name="SendInfo" value="Send" onclick="return val()"/> <br><br>
<input type="button" name="SendInfo" value="Cancel"><br>
</form>
</body>
</html>
答案 0 :(得分:1)
对于表单验证,您应该使用表单的onsubmit事件。
此页面包含一些可以帮助您的示例: https://www.w3schools.com/js/js_validation.asp
使用Javascript:
function validateForm() {
var x = document.forms["myForm"]["fname"].value;
if (x == "") {
alert("Name must be filled out");
return false;
}
}
HTML:
<form name="myForm" action="/action_page_post.php" onsubmit="return validateForm()" method="post">
Name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>