Forum Moderators: open

Message Too Old, No Replies

Run email validation based on if statement

         

Sub_Seven

7:18 am on Aug 7, 2010 (gmt 0)

10+ Year Member



Hello everybody, I am dying inside trying to think I can figure this one out on my own, I think it should be easy for those with more javascript experience.

Explanation: I have a contact form that asks users in what way they would like to be contacted (phone, email or both), these input fields are hidden and are visible onclick, if the user checks the phone radiobutton then the phone cannot be left empty, the email on the other hand is the one giving me the headache because I also want to make sure the email is valid but I can't make the code check if the email is valid only if the radiobutton for the email is checked. Here is the validator code:


function formValidator(){
valid = true;

if ((document.getElementById('contactphone').checked == false) && (document.getElementById('contactemail').checked == false) && (document.getElementById('contactboth').checked == false)){
alert ('check one option test message.');
valid = false;
}

if ((document.getElementById('contactphone').checked == true) && (document.getElementById('phone').value == "") || (document.getElementById('phone').value == null)){
alert ('Phone empty test message.');
valid = false;
}

if ((document.getElementById('contactemail').checked == true) && (document.getElementById('email').value == "") || (document.getElementById('email').value == null)){
alert ('email empty test message.');
}

if ((document.getElementById('contactboth').checked == true) && (document.getElementById('phone').value == "") && (document.getElementById('email').value == "")){
alert ('phone or email test message.');
valid = false;
}

return valid;
}



and I am trying to use this code to make sure the email is valid:


function emailValidator(){
var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
if(elem.value.match(emailExp)){
return true;
}else{
elem.focus();
return false;
}
}


So the question again is: how can I make the formValidator() check or return the emailValidator() only if the email radiobutton is checked or (another way that I think it could work) if the email input field is != "".

Thanks in advance for any help provided.

daveVk

4:28 am on Aug 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



try replacing

return valid;

with

if ((document.getElementById('contactboth').checked == true)||(document.getElementById('contactboth').checked == true)) {
if ( valid===true ) { valid = emailValidator(); }
}
return valid;

in emailValidator add

var elem = document.getElementById('email');

Fotiman

1:09 pm on Aug 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think daveVk meant:

if ((document.getElementById('contactemail').checked == true)||(document.getElementById('contactboth').checked == true)) {
contactemail

Sub_Seven

5:31 am on Aug 11, 2010 (gmt 0)

10+ Year Member



Thanks guys, that's exactly what I need it :)