I have a registration form in HTML. I want to validate it using JavaScript. I want to pass the textbox (not the value) to JavaScript so that I don't have to write document.getElementById("Textname").value
multiple times. I want my JavaScript function to either pick up the HTML active control, or pass the control to the function.
My HTML code:
<i>table>
<tr><td>
First Name</td>><td><input type="text" id="fname" runat="server" /></td></tr>
<tr><td>last Name</td>><td><input type="text" id="lname" runat="server" /></td></tr>
<tr><td>Phone</td>><td><input type="text" id="phone" runat="server" /></td></tr>
<tr><td>email</td>><td><input type="text" id="email" runat="server" /></td></tr>
<tr><td>password</td>><td><input type="password" id="pass" runat="server" /></td></tr>
<tr><td>image</td>><td><input type="file" id="image" runat="server" /></td></tr>
<tr><td rowspan="2">Sex</td>><td>Male<input type="radio" id="male" runat="server" value="male" /></td></tr>
<tr><td colspan=".5">female<input type="radio" runat="server" id="female" /></td></tr>
<tr><td>submit</td>><td><input type="submit" id="submit" runat="server" value="SUBMIT"/></td></tr>
<tr><td>reset</td>><td><input type="reset" id="res" runat="server" /></td></tr>
<tr><td><button id="save" runat="server" value="save" ></button></td></tr>
</table><i/>
JavaScript code:
function cont(textbox)
{
var name=document.getelementbyId(textbox).value;
var illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/;
var regletter = /[a-zA-z]/;
if (uname == "")
{
alert("please enter ");
document.getelementbyid(textbox)style.background = "grey";
document.getElementById(textbox).focus;
}
You can store it into a var, this way you don't have to use document.getElementById
many times.
var element = document.getElementById(textbox);
var name = element.value;
var illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/;
var regletter = /[a-zA-z]/;
if (uname == "") // are you sure it's `uname` and not `name` ?
{
alert("please enter ");
element.background = "grey";
element.focus();
}
Without checking the validity of the code you supplied, the solution is rather simple:
function cont(textbox)
{
var name = textbox.value;
var illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/;
var regletter = /[a-zA-z]/;
if (uname == "") {
alert("please enter ");
textbox.style.background = "grey";
textbox.focus;
}
// ...
}
// execute the function
cont(document.getElementById('Textname'));
Got the answer . To Avoid using Repetitive Code use 'this'. 'this' refer to the Current Control. Below is a small example of using this.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" >
function chk(a) {
alert(a.value);
}
</script>
<title></title>
</head>
<body>
<form id="f1" method="post" action="Default.aspx" >
<input type="text" id="num1" onblur="chk(this)" />
</form>
</body>
</html>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.