Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9002355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:33:35+00:00 2026-06-16T00:33:35+00:00

Trying to add a customer jQuery validator method that basically checks the card type

  • 0

Trying to add a customer jQuery validator method that basically checks the card type and the credit card number for validity.

ie. If the type is MasterCard, if the card number doesn’t start with 5, it’s invalid or if the type is Visa, if the card number doesn’t start with 4, it’s invalid. My company only charges MC or Visa, so no others are needed.

$.validator.addMethod("CCNumber", function(value, element, param) {
  if (param == "MasterCard") {
    return value.substring(0,1) != 5;   
  }
  else if (param == "Visa") {
    return value.substring(0,1) != 4;   
  }
});

Calling it here:

cardnumber: {
  required: true,
  creditcard: true,
  minlength: 13,
  maxlength: 16,
  digits: true,
  CCNumber: function(){ return $('#cardtype').val(); }
},

And the message…

cardnumber: {
  required: "Please enter a valid credit card number",
  minlength: "Please enter a valid credit card number",
  maxlength: "Please enter a valid credit card number",
  digits: "Your credit card number cannot contain spaces.",
  CCNumber: "WRONG"
},

Finally, the HTML:

<select name="cardtype" id="cardtype">
  <option value="MasterCard">MasterCard</option>
  <option value="Visa">Visa</option>
</select>

I’ve never written a jQuery method before, but nothing is happening here. I’m not entirely sure what I’ve done wrong, as I’ve tried to look at other methods, but couldn’t find anything.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:33:36+00:00Added an answer on June 16, 2026 at 12:33 am

    The jQuery Validation plugin already has additional methods available for things like this.

    The following is the default method used in the “Additional Methods” file, but then edited down for only Visa & Mastercard.

    jQuery.validator.addMethod("creditcardtypes", function(value, element, param) {
        if (/[^0-9-]+/.test(value)) {
            return false;
        }
    
        value = value.replace(/\D/g, "");
    
        var validTypes = 0x0000;
    
        if (param.mastercard)
            validTypes |= 0x0001;
        if (param.visa)
            validTypes |= 0x0002;
    
        if (validTypes & 0x0001 && /^(5[12345])/.test(value)) { //mastercard
            return value.length == 16;
        }
        if (validTypes & 0x0002 && /^(4)/.test(value)) { //visa
            return value.length == 16;
        }
        return false;
    }, "Please enter a valid credit card number.");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just trying to add 2 columns to customer table alter table CUSTOMER ADD
I'm trying to extend Button to add a RightClick event. My customer wants a
Trying to add init parameter names to a list in init(ServletConfig) method. public void
I'm having an issue with trying to add a quantity to a product that
I have created 4 jQuery Dialog's like below shows: <div id=addCustomer title=Add Customer> <h2>Add
I am trying to add records into two tables below, CREATE TABLE customer (Custno
I'm trying to add a ticketing system to a custom back-end that powers the
I am trying to add the customer's group to the pdf packing slips. I
Trying to add a column with a custom attribute to the admin customer grid
I am trying to using Netsuite's PHPtoolkip_v2010.php to add new customer records and define

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.