...

View Full Version : Help with forms



phillykid
11-08-2004, 03:33 AM
Here is my problem,
I have the user enter their email and then click on a button to validate it. Then using the length property of the string, I need to determine if the user has entered a value. And if they haven't, alert them that they need to. Then using the indexOfString method, I need to determine if the @ symbol is in their value, and if there is tell them that their email is valid, or if not, tell them it is invalid.
Here is the script i have so far. Basically I am confused by the length propery and indexofString, I can't seem to get them to work properly. Thanks.

<html>
<script language="Javascript">

function checkForm(){
var usersEmail=document.address.myEmail.value;

if (userEmail=""){


alert("Please fill out Email");
}
else{
alert(usersEmail);
document.address.myEmail.value="";
}
}
</script>


<form name="address">
<table border="1" align="center">
<tr>
<td>Email:
</td>
<td>
<input type="text" name="myEmail" size="20"></td>
</tr>

<tr>
<td colspan="2" align="center">
<input type="button" name="runCode" value="Validate" onClick="checkForm()"></td></tr>
<table>
</table>
</form>
</body>
</html>

hemebond
11-08-2004, 06:12 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>47166</title>
</head>
<body>
<form onsubmit="return validate()">
<fieldset>
<label for="email">Email</label>
<input id="email" name="email" type="text" size="20">
<input type="submit">
</fieldset>
</form>
</body>
<script type="text/javascript">
function validate()
{
var email = document.getElementById("email");
if((email.value == '') || (email.value.search(/@/) < 0))
{
throw_error("Please fill out email","email");
return false;
}
else
{
alert(email.value);
email.value = '';
}
return true;
}

function throw_error(msg, obj)
{
alert(msg);
document.getElementById(obj).focus();
}
</script>
</html>

phillykid
11-09-2004, 02:35 AM
thanks, but wouldn't I need to use string.length and string.IndexOf ? This is what I was told to use, but maybe I overlooked it.

Brandoe85
11-09-2004, 03:32 AM
Or you can do this:


<html>
<script language="Javascript">

function checkForm(){
var usersEmail = document.address.myEmail.value;
if (usersEmail == "" || usersEmail.indexOf("@") == -1){
alert("Please fill out Email");
}

else{
alert(usersEmail);
document.address.myEmail.value="";

}
}
</script>
<form name="address">
<table border="1" align="center">
<tr>
<td>Email:
</td>
<td>
<input type="text" name="myEmail" size="20"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="button" name="runCode" value="Validate" onClick="checkForm()"></td></tr>
<table>
</table>
</form>
</body>
</html>

phillykid
11-09-2004, 04:11 AM
ah yes, that works real good.
much thanks!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum