JavaScript Form Validation Tweaks

Script Tweaks on Existing Code

ONE) Check to see there are TWO Names in Contact.value field (with a space in between)

TWO) If AMEX is CCard choice then USER NEEDS 4 numbers in CVV field... existing code that checks this does not work

see attached word doc... Yellow highlighted code needs tweaking...

thanks... Cj
function openMod() {
    //var x = document.getElementById("mySessID").value;
    //alert(x);
    var CardNumber = document.getElementById('<%= CardNumber.ClientID %>');
    var ExMonth = document.getElementById('<%= ExpMonth.ClientID %>');
    var ExYear = document.getElementById('<%= ExpYear.ClientID %>');
    var Address = document.getElementById('<%= address.ClientID %>');
    var State = document.getElementById('<%= state.ClientID %>');
    var Zip = document.getElementById('<%= zip.ClientID %>');
    var company = document.getElementById('<%= company.ClientID %>');
    var contact = document.getElementById('<%= contact.ClientID %>');
    var telephone = document.getElementById('<%= telephone.ClientID %>');
    var city = document.getElementById('<%= city.ClientID %>');
    var fax = document.getElementById('<%= fax.ClientID %>');
    var email = document.getElementById('<%= email.ClientID %>');
    var howhear = document.getElementById('<%= howhear.ClientID %>');
    var CardType = document.getElementById('<%= CardType.ClientID %>');
    var CVV = document.getElementById('<%= CVV.ClientID %>');
    if (company.value == '') {
        alert('Please Enter Your Company Name');
        company.focus();
        return false;
    }
    if (contact.value == '') {
        alert(Please Enter Your FULL Credit Card Signature Name - Both FIRST Name & LAST Name’);
        contact.focus();
        return false;
    }
    if (Address.value == '') {
        alert('Please Enter Your Credit Card Billing Street Address');
        Address.focus();
        return false;
    }
    if (city.value == '') {
        alert('Please Enter Your City');
        city.focus();
        return false;
    }
    if (State.value == '') {
        alert('Please Enter Your State');
        State.focus();
        return false;
    }
    if (Zip.value == '') {
        alert('Please Enter Your Credit Card Billing Zip Code Number');
        Zip.focus();
        return false;
    }
    if (email.value == '') {
        alert('Please Enter Your Email Address Where You Want Us To Send The Data');
        email.focus();
        return false;
    }
    if (telephone.value == '') {
        alert('Please Enter Your Telephone Number');
        telephone.focus();
        return false;
    }
    if (howhear.options[howhear.selectedIndex].text == 'Choose One') {
        alert('Please Select How You Heard About Us - Referral');
        howhear.focus();
        return false;
    }
    if (CardType.options[CardType.selectedIndex].text == 'Choose One') {
        alert('Please Enter Your Credit Card Type');
        howhear.focus();
        return false;
    }
    if (CardNumber.value == '') {
        alert('Please Enter Your Credit Card Number');
        CardNumber.focus();
        return false;
    }
    if (CVV.value == '') {
        alert('Please Enter Your Credit Card CVV Number');
        CVV.focus();
        return false;
    }
    if (CardType.options[CardType.selectedIndex].text == 'AMEX')
        if (CVV.length < '4') {
            alert('Please Enter ALL 4 numbers for an American Express Credit Card CVV Number');
            CVV.focus();
            return false;
        }
    if (ExMonth.options[ExMonth.selectedIndex].text == '??') {
        alert('Please Enter Your Credit Card Expiration Month');
        ExMonth.focus();
        return false;
    }
    if (ExYear.options[ExYear.selectedIndex].text == '??') {
        alert('Please Enter Your Credit Card Expiration Expiration Year');
        ExYear.focus();
        return false;
    }
    var exDate = ExMonth.options[ExMonth.selectedIndex].text + ExYear.options[ExYear.selectedIndex].text;
    $(document).ready(function () {
        $('#basic-modal-content').modal();
    });
    return goToAuthorize(CardNumber.value, exDate, Address.value, State.value, Zip.value);
}

Open in new window

JavaScript-As-Is-Now.doc
EnflowAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sar1973Commented:
Isn't it a simple syntac error...? In the alert function the message is not bracketed:
        {
            alert("Please Enter Your FULL Credit Card Signature Name - Both FIRST Name & LAST Name");
            contact.focus();      
            return false;
        }
instead of:
        {
            alert(Please Enter Your FULL Credit Card Signature Name - Both FIRST Name & LAST Name’ );
            contact.focus();      
            return false;
        }
0
RobOwner (Aidellio)Commented:
What sar1973 means is that the single quote is missing from the beginning of your alert message. (Double quotes fine as well)
0
RobOwner (Aidellio)Commented:
As for the cvv, you need to check for the length of the value not the object. E.g.

If(CVV.value.length < 4) {
Instead of
If(CVV.length < 4) {
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

GaryCommented:
Check name has a space (two words)
   if (contact.value == '' || contact.value.indexOf(" ")<1) {
        alert('Please Enter Your FULL Credit Card Signature Name - Both FIRST Name & LAST Name’);
        contact.focus();
        return false;
    }


Amex length check
   if (CardType.options[CardType.selectedIndex].text == 'AMEX')
        if (CVV.value.length !=4) {
            alert('Please Enter 4 numbers for an American Express Credit Card CVV Number');
            CVV.focus();
            return false;
        }


I would also change your initial CVV check to this
   if (cvv.value.length!=3 && CardType.options[CardType.selectedIndex].text != 'AMEX') {
        alert('Please Enter Your 3 digit Credit Card CVV Number');
        CVV.focus();
        return false;
    }


tagit and Sar1973 both brought up syntax errors which I incorporated.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
EnflowAuthor Commented:
Sar1973...  Sorry...
In my attached file i FORGOT to put in the single Apostrophe at the beginning of the Alert text after i edited it after i copy pasted it... but in my LIVE code the single ' is there at both ends where needed and the alert works... that never was the problem... but

but... my question was HOW do i TEST for TWO Names being in one text box with a SPACE... so i know the user has entered their First and Last names...


TAGIT...
From you solution in CVV length test of  ---If(CVV.value.length < 4) {   -  I was able to also note i should have changed my original code from...

 if (CardType.options[CardType.selectedIndex].text == 'AMEX')
        if (CVV.length < '4') {
            alert('Please Enter ALL 4 numbers for an American Express Credit Card CVV Number');
            CVV.focus();
            return false;
        }

and changed it to...

if(CardType.options[CardType.selectedIndex].value == 'American Express')
                  if(CVV.value.length < 4) {
                        alert('Please Enter ALL 4 numbers for an American Express Credit Card CVV Number');
                        CVV.focus();
                        return false;
        }      

which not only incorporated your solution but also changed .text to .value in first if statement and 'AMEX' to American Express which is the correct value... 100 points... thanks...


Gary123...
tried your solution... it looks right but the Javascript function as whole does not work if i incorporate it...

here is your code inline...

 function openMod()
    {  
        //var x = document.getElementById("mySessID").value;
        //alert(x);
        var CardNumber = document.getElementById('<%= CardNumber.ClientID %>');
        var ExMonth = document.getElementById('<%= ExpMonth.ClientID %>');        
        var ExYear = document.getElementById('<%= ExpYear.ClientID %>');
        var Address = document.getElementById('<%= address.ClientID %>');
        var State = document.getElementById('<%= state.ClientID %>');
        var Zip = document.getElementById('<%= zip.ClientID %>');      
        var company = document.getElementById('<%= company.ClientID %>');
        var contact = document.getElementById('<%= contact.ClientID %>');
        var telephone = document.getElementById('<%= telephone.ClientID %>');
        var city = document.getElementById('<%= city.ClientID %>');
        var fax = document.getElementById('<%= fax.ClientID %>');
        var email = document.getElementById('<%= email.ClientID %>');
        var howhear = document.getElementById('<%= howhear.ClientID %>');
        var CardType = document.getElementById('<%= CardType.ClientID %>');            
        var CVV = document.getElementById('<%= CVV.ClientID %>');
            
        if(company.value == '')
        {
            alert('Please Enter Your Company Name');
            company.focus();
            return false;
        }
            
            if (contact.value == '' || contact.value.indexOf(" ")<1)
            {
                  alert('Please Enter Your FULL Credit Card Signature Name - Both FIRST Name & LAST Name’);
                  contact.focus();
                  return false;
        }
            
            if(Address.value == '')
        {
            alert('Please Enter Your Credit Card Billing Street Address');
            Address.focus();
            return false;
        }

etc...

thanks... Cj

CJ
0
GaryCommented:
Here
alert('Please Enter Your FULL Credit Card Signature Name - Both FIRST Name & LAST Name’);

You have a different apostrophe at the beginning as compared to the end

My fault, change it to
alert('Please Enter Your FULL Credit Card Signature Name - Both FIRST Name & LAST Name');
0
EnflowAuthor Commented:
Bingo ! - Perfectamundo...

thanks GaryC...  :o)

Okay... Am I able to divide the points like this...

125 points - Gary C123
100 points - Tagit
25 points - Sar1973

CJ
0
EnflowAuthor Commented:
thanks to all..

will be back soon with an Ajax JavaScript problem... CJ...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.