MainStWebGuy
06-02-2009, 08:29 PM
Hey guys, i'm just starting to learn javascript and know next to nothing about it.
A friend of mine was helping me with validating a form and gave me the following script (which works like a charm) to make sure the user enters either a phone number or email address before submitting the form:
function checkPhoneEmail() {
if ((document.getElementById('email').value=='' || document.getElementById('email').value==null) && (document.getElementById('phone').value=='' || document.getElementById('phone').value==null)) {
alert("You must fill in a contact mehod: Phone or Email");
return false;
}
else { return true; }
}
I also want to make sure the user provides a name on the form before submitting so i thought i might try to rework the code my friend did, but i can't get it going and really don't know where to start. I'm sure it's something simple, but being so new to all this, nothing seems simple haha.
my code:
function checkName() {
if (document.getElementById('name').value=='' || document.getElementById('name').value==null) {
alert("Please provide your name.");
return false;
}
else { return true; }
}
my Form:
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST" onsubmit="return checkPhoneEmail()">
<label>Name: <input type="text" name="name" id="name" /></label><br />
<label>Email: <input type="text" name="email" id="email" /></label><br />
<label>Phone: <input type="text" name="phone" id="phone"></label><br>
<input type="submit" name="submit" value="Go!!" onSubmit="return checkName()" />
</form>
Where'd did i go wrong?
Thanks in advance for helping another newbie learn a bit!
J
A friend of mine was helping me with validating a form and gave me the following script (which works like a charm) to make sure the user enters either a phone number or email address before submitting the form:
function checkPhoneEmail() {
if ((document.getElementById('email').value=='' || document.getElementById('email').value==null) && (document.getElementById('phone').value=='' || document.getElementById('phone').value==null)) {
alert("You must fill in a contact mehod: Phone or Email");
return false;
}
else { return true; }
}
I also want to make sure the user provides a name on the form before submitting so i thought i might try to rework the code my friend did, but i can't get it going and really don't know where to start. I'm sure it's something simple, but being so new to all this, nothing seems simple haha.
my code:
function checkName() {
if (document.getElementById('name').value=='' || document.getElementById('name').value==null) {
alert("Please provide your name.");
return false;
}
else { return true; }
}
my Form:
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST" onsubmit="return checkPhoneEmail()">
<label>Name: <input type="text" name="name" id="name" /></label><br />
<label>Email: <input type="text" name="email" id="email" /></label><br />
<label>Phone: <input type="text" name="phone" id="phone"></label><br>
<input type="submit" name="submit" value="Go!!" onSubmit="return checkName()" />
</form>
Where'd did i go wrong?
Thanks in advance for helping another newbie learn a bit!
J