Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check field if empty

Status
Not open for further replies.

murphyhg

Technical User
Mar 1, 2006
98
US
I have something that is best handled by javascript. On submit I need to check text inputs form.password & form.password2 and if they are both not empty I need to check to see if radio button resetpassword is checked for yes. If radio button resetpassword is not set to yes I need to send an alert box. If it is set to yes I don't need to do anything.

Here is my starting code. Can you please help me get going with this.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns="<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">
if (document.form.password.value)!="" && document.form.password2.value)!="") {
alert("I am an alert box!!");
else
{
alert("I am an alert box!!");
}
</script>
</head>
<body>
<form method="post" onsubmit="">
Password: <input name="password" type="text" /><br />
Confirm Password: <input name="password2" type="text" /><br />
Reset Password:Yes <input name="resetpassword" type="radio" value="1" /> No <input name="resetpassword" type="radio" value="0" checked />
</form>
</body>
</html>
 

They have an excellent series of tutorials covering both HTML forms and Javascript.

Where did you get the starting code from?

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]

Webflo
 
Your JavaScript has 2 major errors. See below:

Code:
if (document.form.password.value[red])[/red]!="" && document.form.password2.value[red])[/red]!="")


- Lowet

[gray]Why can't all browsers parse pages the same way? It should be the Web designer who decides how to display the content, not the browser![/gray]
 
Thanks, guys that is where I got this code from. I am a total JavaScript Novice. That is why I asked for your help.

Thanks!

I changed the code but I am getting additional errors.
<script language="javascript">
if (document.form.password.value != "" && document.form.password2.value != "") {
alert("I am an alert box!!");
else
{
alert("I am an alert box!!");
}
}
</script>

I need to put this in a function so I can call it on submit.
 
You don't have a form named 'form'.

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]

Webflo
 
Hi Guys I am further along. I am not getting any errors;however I am not getting the expected results. Do, I need to call the second function in the first function?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns="<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">
function message() {
if (document.form.password.value != "" && document.form.password2.value != "") {
if (resetpassword.value == 0) {
confirm("If you want to change your password you must set the radio button reset password to yes");
}
else
{
return;
}
}
else
{
//alert("I am empty");
return;
}
}
function getSelectedRadioValue(resetpassword) {
// returns the value of the selected radio button or "" if no button is selected
var i = getSelectedRadio(resetpassword);
if (i == -1) {
return "";
} else {
if (resetpassword) { // Make sure the button group is an array (not just one button)
return resetpassword.value;
} else { // The button group is just the one button, and it is checked
return resetpassword.value;
}
}
} // Ends the "getSelectedRadioValue" function
</script>
</head>
<body>
<form method="post" onsubmit="message();getSelectedRadioValue();" name="form">
Password:
<input name="password" type="text" />
<br />
Confirm Password:
<input name="password2" type="text" />
<br />
Reset Password:Yes
<input name="resetpassword" type="radio" value="1" />
No
<input name="resetpassword" type="radio" value="0" checked />
<br />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
 
Have you checked out some of the online tutorials? It appears you'd be best served by that approach rather than asking questions that are so basic here.

Or is this some kind of schoolwork?

Lee
 
This script is a total mess. You should write your own script, but first, learn JavaScript. Copying/pasting code does not learn you coding and all the errors copied, will hunt you down l8r ;)

- Lowet

[gray]Why can't all browsers parse pages the same way? It should be the Web designer who decides how to display the content, not the browser![/gray]
 
Listen, I am not doing a school project. I came here to ask for help not a lecture. I have been going to the tutorials and copying and pasting code at dwarfthrowers suggestion.
 
I was able to get this working. I want to share it in case another person that is new comes along they can use it as well.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns="<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JavaScript Example</title>
<script language="javascript">
function f_checkPwd() {
var e_form = document.forms['myformname'];
var e_resetPwd = e_form.elements['resetpassword'],
e_pwd = e_form.elements['password'],
e_pwd2 = e_form.elements['password2'];

if (e_resetPwd && e_resetPwd[1].checked && e_pwd.value != '' && e_pwd2.value != '') {
alert("To change your password you need to set the reset password field to yes");
return false;
}
return true;
}
</script>
</head>
<body>
<form method="post" onsubmit="return f_checkPwd();" name="myformname">
Password:
<input name="password" type="text" />
<br />
Confirm Password:
<input name="password2" type="text" />
<br />
Reset Password:Yes
<input name="resetpassword" type="radio" value="1" />
No
<input name="resetpassword" type="radio" value="0" checked />
<br />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top