Click to See Complete Forum and Search --> : form


chappie
02-28-2009, 06:05 PM
//copied from JS as is
<form name="input" action="html_form_action.asp" method="get">

when user clicks on submit, i want to validate email by calling JS validateEmail(email) in the action 'field', can i do that? and do i set method to 'get' or 'post'?

//modified
<form name="input" action="validateEmail()" method="post">

Charles
02-28-2009, 06:31 PM
<script type="text/javascript">
String.prototype.isEmailAddress = function () {return /^[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+$/.test(this)}

onload = function () {
document.forms[0].onsubmit = function () {
var e, i = 0
while (e = this.elements[i++]) {
if ((e.type == 'text' || e.type == 'textarea') && !/\S/.test(e.value)) {
alert ('Please, all fields are required.');
return false;
} else {
this.elements.hidden.value = 'verified';
}
}
}

document.forms[0].elements.address.onchange = function () {
if (!this.value.isEmailAddress()) {
alert ('That would not appear to be a valid email address.');
this.value='';
this.focus();
}
}
}
</script>

<form name="input" action="html_form_action.asp" method="post">
<fieldset>
<legend>Send Us a Message</legend>

<label>Your e-mail address:<input name="address" type="text" value=""></label>
<label>Your subject:<input name="subject" type="text" value=""></label>

<label>Your message:<textarea cols="20" name="message" rows="10"></textarea></label>
<button type="submit">Send</button>
</fieldset>
</form>

rpgfan3233
02-28-2009, 06:43 PM
You would use the onsubmit attribute:
<form name="input" action="your_submission_url.php" onsubmit="validateEmail()" method="post">

Charles
03-01-2009, 06:07 AM
You would use the onsubmit attribute:
<form name="input" action="your_submission_url.php" onsubmit="validateEmail()" method="post">If you do it that way you will need<form name="input" action="your_submission_url.php" onsubmit="validateEmail(); return false" method="post">Or perhaps<form name="input" action="your_submission_url.php" onsubmit="return validateEmail(this)" method="post">