...

View Full Version : Help with javascript validation



sammyp
05-21-2012, 01:41 AM
I am new to this forum.. and I am hoping to get help with this javascript validation for my form for my assignment. trying to validate multiple fields. I have never done javascript, so with my limited understanding from Google, I put together code snippets.

Problems:
1. How can I get one message box when the user presses submit indicating all failed validation at one time
2. The expired credit card validation doesnt work
3. Cant seem to find a good "confirm password" script
3. The form does not submit if all the fields are correct.


<script language="javascript">
<!--
function checkAge()
{
var bday=parseInt(document.forms[0].txtBday.value);
var bmo=(parseInt(document.forms[0].txtBmo.value)-1);
var byr=parseInt(document.forms[0].txtByr.value);
var byr;
var age;
var now = new Date();
tday=now.getDate();
tmo=(now.getMonth());
tyr=(now.getFullYear());

{
if((tmo > bmo)||(tmo==bmo & tday>=bday))
{
age=(tyr-byr)
}

else
{
age=(tyr-(byr+1))
}

if (age<18)
{
alert ("you must be over 18 years");
return false;

}

else
{
return true;
}
}
}
//-->





function checkEmail()
{
var x=document.getElementById("email").value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
alert("Not a valid e-mail address");
return false;
}
{
return true;
}
}




function onlyNumbers(evt)
{
var e = event || evt; // for trans-browser compatibility
var charCode = e.which || e.keyCode;

if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;

{
return true;
}



}







function checkCardNumber()
{
var x = document.getElementById("cardNumber").value;

if (x.length<16)
{
alert("Your Credit Card Number must be 16 digits");
return false;
}
{
return true;
}
}



function checkSecurityCode()
{
var y = document.getElementById("securityCode").value;

if (y.length<3)
{
alert("Your Security number must be three digits");
return false;


}
{
return true;
}
}


function ValidateExpDate()

{

var ccExpYear = 20 + $F('<%= expYear.ClientID%>');

var ccExpMonth = $F('<%= expMonth.ClientID%>');



var expDate=new Date();

expDate.setFullYear(ccExpYear, ccExpMonth, 1);



var today = new Date();



if (expDate<today)

{
alert("card expired");


// Credit Card is expire

return false;

}

else

{

// Credit is valid

return true;

}

}


function runall()
{

checkAge();
checkEmail();
checkCardNumber();
checkSecurityCode();



{
return false;}




}







</script>

Philip M
05-21-2012, 07:40 AM
When posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post. Most people will ignore it otherwise.

There are a large number of errors, and much of your code is completely obsolete. You need to have a look at regular expressions. <script language=javascript> is long deprecated. Use <script type = "text/javascript"> instead (in fact also deprecated but still necessary for IE<9). The <!-- and //--> comment (hiding) tags have not been necessary since IE3 (i.e. since September 1997). If you see these in some published script it is a warning that you are looking at ancient and perhaps unreliable code.

Form validation has been covered many times before in this forum. Suggest you use the search feature.

Example:-

var y = document.getElementById("securityCode").value;
if (y.length<3) {
alert("Your Security number must be three digits");

But 12345 or abc will pass the validation! Same with credit card "numbers"!

var ccExpYear = 20 + $F('<%= expYear.ClientID%>'); - You seem to have copied this blindly from some script calling a PHP variable.

To be sensible an age check must take account of year, month and day. You will find code for this in this forum. A credit card expiry date is in the form MM/YY, so you must split it at the / and then compare year and month with the date today.

Confirm password - ask the user to enter it twice, and simply the compare the two entries. The fact that you are not able to create this simple check yourself suggests that you are out of your depth. How is it that you are asked to complete an assignment in Javascript when you have never "done" (studied?) it? That makes little sense.

Sorry, but your code really needs a complete re-write. And, if I may say so, more serious understanding of Javascript. If felgall saw it he would have kittens! :D

It could end up 3-0 to either side if Newcastle get an equaliser. - Commentator Radio 5 Live

sammyp
05-21-2012, 12:58 PM
Well it is true that I am totally "out of my depth". I am in my 2nd yr bachelors in IT, and all of a sudden we are asked to do javascript. It was never taught to us, and when asked, we are told we are on our own...

I am a bit embarrased at the "obsolete code", and at times it was infact copied blindly...but I literally had no choice

Sorry for the bother...I will try to search this forum for solutions.

Philip M
05-21-2012, 01:43 PM
Well it is true that I am totally "out of my depth". I am in my 2nd yr bachelors in IT, and all of a sudden we are asked to do javascript. It was never taught to us, and when asked, we are told we are on our own...

I am a bit embarrased at the "obsolete code", and at times it was infact copied blindly...but I literally had no choice

Sorry for the bother...I will try to search this forum for solutions.

I wonder if the same abysmal standard of "teaching" applies to other subjects at your university, say physics or medicine or law.

sammyp
05-22-2012, 11:35 AM
Well actually other than the javascript part, the programme has been quite good. I would not know about the other programs at the school, but I dont think it is necesssary for you to speculate so negatively in that manner.

Nevertheless, at least I am trying, and may have got it horribly wrong at the first try, but I will get it right in time. Javascript couldnt be that hard.

Thanks again for your help Phillip.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum