Javascript文本字段验证 [英] Javascript text field validation
问题描述
我在使用javascript验证表单时遇到了困难。我目前正在检查一个文本字段,它不起作用。我的代码如下:
index.html:
< ; html xmlns =http://www.w3.org/1999/xhtml\">
< head>
< title>
验证表格
< / title>
< script type =text / javascriptsrc =vForm.js>
< / script>
< / head>
< body>
< form id =myFormaction =>
名字:< input type =textname =fname>< / br>
姓氏:< input type =textname =lname>< / br>
密码:< input type =passwordname =pass1>< / br>
重新输入密码:< input type =passwordname =pass2>< / br>
电子邮件地址:< input type =textname =email>< / br>
电话:< input type =textname =phone>< / br>
地址:< input type =textname =add>< / br>
日期:< input type =datename =date>< / br>
时间:< input type =timename =time>< / br>
< input type =resetname =reset>
< input type =submitname =submit>
< / form>
< script type =text / javascriptsrc =vFormRun.js>
< / script>
< / body>
< / html>
vForm.js:
function validateForm()
{
var fname = document.getElementById(fname);
var lname = document.getElementById(lname);
var pass1 = document.getElementById(pass1);
var pass2 = document.getElementById(pass2);
var email = document.getElementById(email);
if(fname ==)
{
alert(请输入名字)
return false;
}
else
{
return true;
vFormRun.js:
document.getElementById(myForm)。onsubmit = validateForm;
文件。的getElementById(其中 fname);
只有当元素的ID为 fname
,你不这样做。
你可以将ID属性设置为如下元素:
< input type =textname =fnameid =fname>
或者,您可以像这样引用表单元素:
var fname = document.forms [myForm] [fname]
然后,您想在比较时获得 value
属性。
fname.value
< br>
标记是自动关闭的,所以它应该是< br />
,而不是< / br>
I am having difficulty validating a form in javascript. I'm currently checking just a text field and it doesn't work. My code is as followed:
index.html:
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>
Validation Form
</title>
<script type = "text/javascript" src ="vForm.js">
</script>
</head>
<body>
<form id = "myForm" action ="">
First name: <input type="text" name="fname"></br>
Last name: <input type="text" name="lname"></br>
Password: <input type="password" name="pass1"></br>
Re-enter password: <input type="password" name="pass2"></br>
Email: <input type="text" name="email"></br>
Phone: <input type="text" name="phone"></br>
Address: <input type="text" name="add"></br>
Date: <input type="date" name="date"></br>
Time: <input type="time" name="time"></br>
<input type="reset" name="reset">
<input type="submit" name="submit">
</form>
<script type = "text/javascript" src ="vFormRun.js">
</script>
</body>
</html>
vForm.js:
function validateForm()
{
var fname = document.getElementById("fname");
var lname = document.getElementById("lname");
var pass1 = document.getElementById("pass1");
var pass2 = document.getElementById("pass2");
var email = document.getElementById("email");
if(fname == "")
{
alert("Please enter first name")
return false;
}
else
{
return true;
}
}
vFormRun.js:
document.getElementById("myForm").onsubmit = validateForm;
document.getElementById("fname");
That will only work if you have an element with an ID of fname
, which you do not.
You can set the ID attribute to an element like so:
<input type="text" name="fname" id="fname">
Alternatively, you can reference the form elements like this:
var fname = document.forms["myForm"]["fname"]
Then you want to get it's value
property when comparing.
fname.value
The <br>
tag is self closing, so it should be <br />
instead of </br>
这篇关于Javascript文本字段验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!