Forum Moderators: open
Javascript:
<script language="text/javascript" charset="utf-8">
function validateForm(RegForm){
var userName = RegForm.myEmail.value;
var password = RegForm.myPass.value;
if (userName.length === 0) {
alert("You must enter a username (Valid Email).");
return false;
}
if (password.length === 0) {
alert("You must enter a password.");
return false;
}
return true;
</script>
xHTML Form:
<form ACTION="MyAction" METHOD="POST" name="RegForm" id="RegForm">
<fieldset>
<legend>Input your details to register </legend>
<p><label for="name">Email address:</label> <input name="myEmail" type="text" id="name" />
</p>
<p><label for="name">Choose password:</label> <input name="myPass" type="password" id="name" />
</p>
<p class="submit"><input name="submit" type="submit" id="submit" onClick="return validateForm();" value="Register now" />
</p><br />
</fieldset>
</form>
Thank you for any help you can provide.
<script type="text/javascript">
<form ACTION="MyAction" METHOD="POST" name="RegForm" id="RegForm" onsubmit="return validateForm(this);">
<script type="text/javascript" src="yourfile.js"></script>
<form action="MyAction" method="POST" name="RegForm" id="RegForm">
<fieldset>
<legend>Input your details to register </legend>
<p>
<label for="myEmail">Email address:</label>
<input name="myEmail" id="myEmail" type="text" />
</p>
<p>
<label for="myPass">Choose password:</label>
<input name="myPass" id="myPass" type="password" />
</p>
<p class="submit">
<input name="submitbtn" id="submitbtn" type="submit" value="Register now" />
</p>
<br />
</fieldset>
</form>
And you would include script separately that would then get the "RegForm" and attach an onsubmit listener or handler.
Any questions?
I've gone through it using your message above as reference but haven't seen anything wrong with it. Although I am quite tired now as I've been working on this all day and it's now 22:30h where I am :(
Script in seperate page here: <script type="text/javascript" src="MyValidation.js"></script>
function validateForm(RegForm){
var firstName = RegForm.FirstName.value;
var userName = RegForm.dEmail.value;
var password = RegForm.dPassword.value;
if (firstName.length === 0) {
alert("Please enter a first name.");
return false;
}
if (userName.length === 0) {
alert("Please enter a username (Valid Email).");
return false;
}
if (password.length === 0) {
alert("Please enter a password.");
return false;
}
return true;
}
Form here:
<form ACTION="MyAction" METHOD="POST" name="RegForm" id="RegForm" onsubmit="return validateForm(this);">
<fieldset>
<legend>Input your details to register </legend>
<p><label for="name">First Name :</label> <input name="FirstName" type="text" id="FirstName" />
</p>
<p><label for="name">Last Name :</label> <input name="LastName" type="text" id="LastName" />
</p>
<p><label for="name">Email address:</label> <input name="dEmail" type="text" id="dEmail" />
</p>
<p><label for="name">Choose password:</label> <input name="dPassword" type="password" id="dPassword" />
</p>
<p class="submit"><input name="submitButton" type="submit" id="submitButton" value="Register now" />
</p><br />
</fieldset>
</form>
<html>
<head>
<title>Test</title>
</head>
<body>
<form action="MyAction" method="POST" name="RegForm" id="RegForm" onsubmit="return validateForm(this);">
<fieldset>
<legend>Input your details to register </legend>
<p>
<label for="name">First Name :</label>
<input name="FirstName" type="text" id="FirstName" />
</p>
<p>
<label for="name">Last Name :</label>
<input name="LastName" type="text" id="LastName" />
</p>
<p>
<label for="name">Email address:</label>
<input name="dEmail" type="text" id="dEmail" />
</p>
<p>
<label for="name">Choose password:</label>
<input name="dPassword" type="password" id="dPassword" />
</p>
<p class="submit">
<input name="submitButton" type="submit" id="submitButton" value="Register now" />
</p>
<br />
</fieldset>
</form>
<script type="text/javascript">
function validateForm(RegForm) {
var firstName = RegForm.FirstName.value;
var userName = RegForm.dEmail.value;
var password = RegForm.dPassword.value;
if (firstName.length === 0) {
alert("Please enter a first name.");
return false;
}
if (userName.length === 0) {
alert("Please enter a username (Valid Email).");
return false;
}
if (password.length === 0) {
alert("Please enter a password.");
return false;
}
return true;
}
</script>
</body>
</html>
(I'm only including the script in the HTML here to make this example more portable).
I went through everything and it seems to be fine. I have the script on an .asp page. You don't think that would have any impact do you? Basically when I submit the form it seems to just bypass the script and go onto the next page. I've no idea why as I haven't changed much on it as you can see.
Here is the script I have at the mo but it's not working with the script I have:
function checkEmail(emailAddr)
{
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailAddr.value))
{
return (true)
}
alert("Invalid E-mail Address! Please re-enter.")
emailAddr.focus();
emailAddr.select();
return false;
}