Forum Moderators: open

Message Too Old, No Replies

form validation/dom/event handlers/reg exp

         

quartzy

3:58 pm on Apr 13, 2009 (gmt 0)

10+ Year Member



This form loads OK, and I have tested it with alert, however, it is not validating correctly. Can anyone see why?
The form uses dom and regular expressions, and event handlers. I am new to all this. But have to complete this form for a test. As far as I know there are no known errors. But when I enter in wrong information, the form does not throw up any error messages.

<code>
function validPostCodeRe (){
return /^[A-Z]{2}[0-9]{1,2}\s[0-9]{1,2}[A-Z]{1,2}$/i.test(validpostcode);
}
function validSpecialPostCodeRe (){
return /^([M][K][1-15]{1,2})¦([M][K][1][7])¦([M][K][1][9])¦([M][K][7]{2})\s[0-9]{1,2}[A-Z]{1,2}$/i.test(validspecialpostcode);
}
function validFcardRe () {
return /^\W{13}\d[!&\?@]{1}$/i.test(validfcard);
}

function verify(evt) {
function get(id) {
return document.getElementById(id); }
var message = 'Please correct invalid input:<br />';
var messageNode = get('intro');
var valid = true;
var postcode = get('postcode').value;
var specialpostcode = get('postcode').value;
var fcard = get('fidelity card').value;

if (!validPostCode()){
valid=false;
message.push('Postcode is not valid');

}
if (!validSpecialPostCode()){
valid = false;
message.push('Postcode is not valid');
}
else if (validSpecialPostCode ()){
valid = true;
alert ("You will received a special gift on submission");
}

if (!validFcard ()){
valid=false;
message.push('Card is not valid');
}
if (!valid) {
evt.preventDefault();
}
else
while(messageNode.childNodes.length>0) messageNode.removeChild(messageNode.childNodes[0]);
for(var i=0; i < message.length; i++) {
messageNode.appendChild(document.createTextNode(message[i]) );
messageNode.appendChild(document.createElement('br') );
messageNode.removeChild(messageNode.lastChild);
};
return valid;
}
// end of verify

window.onload = function() {
if (window.addEventListener) {
document.getElementById('submit').addEventListener('click', verify, false );
}
}
</code>

quartzy

4:02 pm on Apr 13, 2009 (gmt 0)

10+ Year Member



I also have this in the html

<input id="submit" type="submit" value="Send details" onclick="return verify();"/>