Imaginx
asked on
Looking for an "onblur" java function to verify an email address within a form.
I'm trying to find an onblur function that will verify an email address before the submit button is pressed.
The page loads a flash file, and I don't want to have to reload the flash to have php verify the information.
Seems as if java is going to be the most lightweight way to do this.
The page loads a flash file, and I don't want to have to reload the flash to have php verify the information.
Seems as if java is going to be the most lightweight way to do this.
above solution uses form submit event, not onblur... I guess submit event is the way to go...
if user never clicks email, onblur never fires... makes code not useful... so, use submit event of the form...
if user never clicks email, onblur never fires... makes code not useful... so, use submit event of the form...
But pass the form - so even if the form is not the 0th form on the page it will work
<script>
function validateForm(theForm){
if (theForm.txtEmail.value == ""){
alert("Please enter email!");
theForm.txtEmail.focus();
return false;
}
return true;
}
</script>
<form action="http://goggle.ca" method=post onSubmit="return validateForm(this);">
<input type=text name=txtEmail>
<input type=submit value=Submit>
</form>
ASKER
That just verifies the field isn't blank though, which is still useful over the php method - but it doesn't verify an email address..
And I probably wasn't as clear as I should have been. I shouldn't use the word "verify" because I obviously don't want it to attempt to send an email and wait for an error of some sorts ...
I want it to validate the email format after someone types it in. That's why I was thinking about onblur.
And I probably wasn't as clear as I should have been. I shouldn't use the word "verify" because I obviously don't want it to attempt to send an email and wait for an error of some sorts ...
I want it to validate the email format after someone types it in. That's why I was thinking about onblur.
there are lots of function on google ;) to validate emails, here is one
function validate(address) {
var reg = /^([A-Za-z0-9_\-\.])+\@([A -Za-z0-9_\ -\.])+\.([ A-Za-z]{2, 4})$/;
if(reg.test(address) == false) {
return false;
}
return true;
}
+ above fix
function validate(address) {
var reg = /^([A-Za-z0-9_\-\.])+\@([A
if(reg.test(address) == false) {
return false;
}
return true;
}
+ above fix
<script>
function validate(address) {
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
if(reg.test(address) == false) {
return false;
}
return true;
}
function validateForm(theForm){
if (theForm.txtEmail.value == ""){
alert("Please enter email!");
theForm.txtEmail.focus();
return false;
}
if (!validate(theForm.txtEmail.value))""){
alert("Please enter a valid email!");
theForm.txtEmail.focus();
return false;
}
return true;
}
</script>
<form action="http://goggle.ca" method=post onSubmit="return validateForm(this);">
<input type=text name=txtEmail>
<input type=submit value=Submit>
</form>
oops, small fix
<script>
function validate(address) {
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
if(reg.test(address) == false) {
return false;
}
return true;
}
function validateForm(theForm){
if (theForm.txtEmail.value == ""){
alert("Please enter email!");
theForm.txtEmail.focus();
return false;
}
if (!validate(theForm.txtEmail.value)) {
alert("Please enter a valid email!");
theForm.txtEmail.focus();
return false;
}
return true;
}
</script>
<form action="http://goggle.ca" method=post onSubmit="return validateForm(this);">
<input type=text name=txtEmail>
<input type=submit value=Submit>
</form>
http://regexlib.com/Search.aspx?k=email&c=-1&m=-1&ps=20
var emailRegex = /^([a-zA-Z0-9_\-\.]+)@((\[ [0-9]{1,3} \.[0-9]{1, 3}\.[0-9]{ 1,3}\.)|(( [a-zA-Z0-9 \-]+\.)+)) ([a-zA-Z]{ 2,4}|[0-9] {1,3})(\]? )$/
function isEmail(email) {
return emailRegex.test(email);
}
function validateForm(theForm){
var email = theForm.txtEmail.value;
if (email != "" && !isEmail((email)){ // remove the email!=""&& if you want to disallow empty emails
var emailRegex = /^([a-zA-Z0-9_\-\.]+)@((\[
function isEmail(email) {
return emailRegex.test(email);
}
function validateForm(theForm){
var email = theForm.txtEmail.value;
if (email != "" && !isEmail((email)){ // remove the email!=""&& if you want to disallow empty emails
okee
see http://www.codetoad.com/javascript/form_validation_function.asp
If you have an onsubmit function that returns true, then it submits the form. If false, it won't submit the form, so you can do your validation in the onsubmit button.
<form name=form1 action=address.asp method=post onsubmit="javascript:retur n ValidateForm(this)">
function ValidateForm(form)
{
if(IsEmpty(form.account_nu mber))
{
alert('You have not entered an account number')
form.account_number.focus( );
return false;
}
if (!IsNumeric(form.account_n umber.valu e))
{
alert('Please enter only numbers or decimal points in the account field')
form.account_number.focus( );
return false;
}
return true;
}
function isValidEmail(str) {
return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}
If you have an onsubmit function that returns true, then it submits the form. If false, it won't submit the form, so you can do your validation in the onsubmit button.
<form name=form1 action=address.asp method=post onsubmit="javascript:retur
function ValidateForm(form)
{
if(IsEmpty(form.account_nu
{
alert('You have not entered an account number')
form.account_number.focus(
return false;
}
if (!IsNumeric(form.account_n
{
alert('Please enter only numbers or decimal points in the account field')
form.account_number.focus(
return false;
}
return true;
}
function isValidEmail(str) {
return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}
ASKER
I can get the onSubmit to work, I was looking for an onBlur function that didn't return an alert - if just displayed a message (i.e. document.write ??)
If the the title over the form input that said:
"Please enter your email"
Would possibly turn red & bold if the input failed the test.
If the the title over the form input that said:
"Please enter your email"
Would possibly turn red & bold if the input failed the test.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I thought you could use document.write after load (maybe a rookie mistake, java is not my forte')
What will happen?
What will happen?
No. Document.write will wipe the current page including whatever scripts are on it.
Open in new window