Jump to content

Javascript validation issues


ChrisML123

Recommended Posts

I'm making a javascript script to check that the user has filled in the form correctly. Two problems:

 

1. Email isn't validating properly.

2. I can't seem to get anything except name and email to validate - the other bits just dont do anything.

 

You can see it in action (or not) here: http://names-not-numbers.co.uk/sponsor/

 

Here is my form

<form name="form" id="form" class="form" action="payment.php" onsubmit="return validate(this)" method="post">
<table width="485"  border="0" cellspacing="0" cellpadding="0">
  <tr>
   <td width="216" align="right"><label for="name">Full Name:</label></td>
    <td width="269"><input type="text" name="name" id="name" /></td>
  </tr>
  <tr>
    <td width="216" align="right"><label for="email">Email Address:</label></td>
    <td width="269"><input type="text" name="email" id="email" /></td>
  </tr>  
  <tr>
    <td width="216" align="right"><label for="address">First line of address:</label></td>
    <td width="269"><input type="text" name="address" id="address" /></tr>
  <tr>
    <td align="right"><label for="address2">Second line of address:</label></td>
    <td><input type="text" name="address2" id="address2" />  
  </tr>
  <tr>
    <td align="right"><label for="city">City:</label></td>
    <td><input type="text" name="city" id="city" />  
  </tr>
  <tr>
    <td align="right"><label for="county">County:</label></td>
    <td><input type="text" name="county" id="county" />  
  </tr>
  <tr>
    <td align="right"><label for="postcode">Postcode:</label></td>
    <td><input type="text" name="postcode" id="postcode" />  
  </tr>
  <tr>
    <td align="right"><label>Are you eligible to pay gift aid?</label></td>
    <td><input type="radio" name="giftaid" value=1> Yes
    <input type="radio" name="giftaid" value=0> No
    <br>  
  </tr>  
  <tr>
    <td align="right"><label>Payment method</label></td>
    <td><input type="radio" name="payment" value="online">Online Payment 
    <input type="radio" name="payment" value="cheque"> Cheque 
    <input type="radio" name="payment" value="cash">Cash 
  </tr>
  <tr>
    <td colspan="2" align="center"><INPUT name="send_form" type="submit" value="Submit"> </td>
  </tr>
</table> 

 

 

An here's my script

function validate(form) {
  var name = form.name.value;
  var email = form.email.value;
  var address = form.address.value;  
  var address2 = form.address2.value;
  var city = form.city.value;
  var county = form.county.value;
  var payment = form.payment.value;	
  var giftaid = form.giftaid.value;
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/;
  var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
  if(name == "") {
    inlineMsg('name','You must enter your name.',2);
    return false;
  }
  if(!name.match(nameRegex)) {
    inlineMsg('name','You have entered an invalid name.',2);
    return false;
  }
  if(email == "") {
    inlineMsg('email','<strong>Error</strong><br />You must enter your email.',2);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','<strong>Error</strong><br />You have entered an invalid email.',2);
return false;
  }
   if(address == "") {
alert("I am here");
    inlineMsg('address','Please enter your address.',2);
    return false;
  }
   if(city == "") {
    inlineMsg('city','Please enter a city.',2);
    return false;
  }
   if(county == "") {
    inlineMsg('county','You must enter a county.',2);
    return false;
  }
   if(payment == "") {
    inlineMsg('payment','You must select a payment method.',2);
    return false;
  }
   if(giftaid == "") {
    inlineMsg('giftaid','Please select one option.',2);
    return false;
  }
  return true;
}

// START OF MESSAGE SCRIPT //

var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 3;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
  var msg;
  var msgcontent;
  if(!document.getElementById('msg')) {
    msg = document.createElement('div');
    msg.id = 'msg';
    msgcontent = document.createElement('div');
    msgcontent.id = 'msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('msg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);
  targetdiv.focus();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + 'px';
  msg.style.left = leftposition + 'px';
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;  
  }
  window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('msg');
  if(!msg.timer) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}

// face the message box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}

// preload the arrow //
if(document.images) {
  arrow = new Image(7,80); 
  arrow.src = "images/msg_arrow.gif"; 
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.