admin管理员组文章数量:1397142
Here I have created two fields one is for first name and another is for last name. Now how can I access these fields in external Javascript file and validate them.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Validation</title>
</head>
<body>
<form action="something" method="post">
FirstName:<input type="text" name="fname"/>
LastName:<input type="text" name="lname"/>
<input type="submit" value="submit">
</form>
</body>
</html>
I want to validate both First and last name using external Javascript file. how can i take the values to external Javascript file and validate. Please give me suggestions.
Thanks in advance.**
Here I have created two fields one is for first name and another is for last name. Now how can I access these fields in external Javascript file and validate them.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Validation</title>
</head>
<body>
<form action="something" method="post">
FirstName:<input type="text" name="fname"/>
LastName:<input type="text" name="lname"/>
<input type="submit" value="submit">
</form>
</body>
</html>
I want to validate both First and last name using external Javascript file. how can i take the values to external Javascript file and validate. Please give me suggestions.
Thanks in advance.**
Share Improve this question edited Jan 18, 2017 at 11:35 fernandosavio 10.4k4 gold badges26 silver badges34 bronze badges asked Oct 7, 2013 at 10:27 user2854201user28542012 Answers
Reset to default 2<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Validation</title>
<script type="text/javascript" src="javascript.js"></script>
</head>
<body>
<form name="myform" action="something" onsubmit="return validateForm()" method="post">
FirstName:<input type="text" name="fname"/>
LastName:<input type="text" name="lname"/>
<input type="submit" value="submit">
</form>
</body>
</html>
and your java script file is(save as javascript.js)
function validateForm()
{
var x=document.forms["myform"]["fname"].value;
if(x==null || x=="" )
{
alert("name can't be left blank");
return false;
}
var y=document.forms["myform"]["lname"].value;
if(y==null || y=="")
{
alert("last name is mandatory");
return false;
}
else
{
return true;
}
}
Add an id to your fields and use getElementById
For the input value HTMLInputElement
本文标签: htmlhow to validate form values using external javascriptStack Overflow
版权声明:本文标题:html - how to validate form values using external javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744147943a2592924.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论