• Javascript
  • Python
  • Go

jQuery Regex for EIN Number and SSN Number Formats

jQuery is a popular JavaScript library that is widely used for creating dynamic and interactive web pages. One of the key features of jQuery...

jQuery is a popular JavaScript library that is widely used for creating dynamic and interactive web pages. One of the key features of jQuery is its powerful functionality for working with regular expressions, also known as regex. Regular expressions are a sequence of characters that define a search pattern, allowing developers to search and manipulate text in a variety of ways. In this article, we will explore how jQuery can be used to validate and format two important identification numbers: EIN numbers and SSN numbers.

EIN numbers, or Employer Identification Numbers, are assigned by the Internal Revenue Service (IRS) to identify businesses for tax purposes. These numbers are unique to each business and are typically formatted as nine digits with a dash in the middle (e.g. 12-3456789). SSN numbers, or Social Security Numbers, are issued by the Social Security Administration (SSA) to individuals for identification and social security benefits. They are also nine digits long and are usually formatted with dashes in between (e.g. 123-45-6789).

To begin, let's create a simple HTML form with two input fields for EIN and SSN numbers, as well as a button to submit the form. We will use jQuery to validate the input and format it according to the correct patterns.

First, we will need to include the jQuery library in our HTML document. We can do this by adding the following code inside the <head> tag:

<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>

Next, we will create our form with the necessary input fields and button:


<label for="ein">EIN Number:</label>

<input type="text" id="ein" name="ein" required>

<label for="ssn">SSN Number:</label>

<input type="text" id="ssn" name="ssn" required>

<button type="button" id="submit">Submit</button>


Now, let's add some jQuery code to validate and format the input. We will use the .keyup() function to check the input as the user types and the .blur() function to validate the input when the user clicks outside of the input field.

$(document).ready(function() {

// validate and format EIN number

$("#ein").keyup(function() {

var ein = $(this).val(); // get the input value

var regex = /^(\d{2})-(\d{7})$/; // regex pattern for EIN number

if (regex.test(ein)) { // check if input matches pattern

$(this).val(ein.replace(regex, "$1-$2")); // format input with dash

$(this).removeClass("error"); // remove error class if present

} else {

$(this).addClass("error"); // add error class if input does not match pattern


}).blur(function() {

var ein = $(this).val();

var regex = /^(\d{2})-(\d{7})$/;

if (!regex.test(ein)) { // check if input does not match pattern

$(this).val(""); // clear the input field



// validate and format SSN number

$("#ssn").keyup(function() {

var ssn = $(this).val();

var regex = /^(\d{3})-(\d{2})-(\d{4})$/; // regex pattern for SSN number

if (regex.test(ssn)) {

$(this).val(ssn.replace(regex, "$1-$2-$3"));


} else {



}).blur(function() {

var ssn = $(this).val();

var regex = /^(\d{3})-(\d{2})-(\d{4})$/;

if (!regex.test(ssn)) {




// submit form on button click

$("#submit").click(function() {

if ($(".error").length) { // check if there are any inputs with error class

alert("Please enter valid EIN and SSN numbers."); // show error message

} else {

alert("Form submitted successfully!"); // show success message

$("form")[0].reset(); // reset the form




Let's break down the jQuery code. First, we use the .keyup() function to check the input values as the user types. Inside the function, we get the input value using the .val() method and assign it to a variable. Then, we define a regex pattern for each number format and use the .test() method to check if the input matches the pattern. If it does

Related Articles

Regex: [A-Za-z][A-Za-z0-9]{4}

Regular expressions, commonly referred to as regex, are powerful tools used for pattern matching and manipulation in various programming lan...