Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with javascript validation

    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>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    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!

    It could end up 3-0 to either side if Newcastle get an equaliser. - Commentator Radio 5 Live
    Last edited by Philip M; 05-21-2012 at 08:29 AM. Reason: Noticed typo

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks..

    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.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by sammyp View Post
    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New to the CF scene
    Join Date
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •