We help IT Professionals succeed at work.

Check out this week's podcast, "Dairy Farms to Databases: Community's Hand in Technology"Listen Now


javascript check input pattern and add class if correct pattern

movieprodw asked
Last Modified: 2017-11-21

I would like to use javascript/jquery to see if the input of a text field fits the serial number pattern, if so I want it to add the class 'snmatch' if not I want it to have the css class 'nosnmatch'

	.nosnmatch { border: solid 1px blue; }
	.snmatch { border: solid 1px green;	}
<form method="post" id="sncheck">
	<input type="text" class="nosnmatch" name="snumber" placeholder="Please enter your serial number PPR0000000" />

Open in new window

Watch Question

Distinguished Expert 2017

Comments inside the codes:
	.nosnmatch { border: solid 2px blue; }
	.snmatch { border: solid 2px green;	}
<form method="post" id="sncheck">
<!--I set an id attr to make my life simpler-->
	<input type="text" class="nosnmatch" id="check" name="snumber" placeholder="Please enter your serial number PPR0000000" />

Open in new window

JQuery code:

//Via JQuery I target the input element.I use keydown so
//to get and the backspace key function
   var keyNumber,keyLenght;
//At the following line I get the value of the input element
//every time that i have keydown.I trimmed the string to remove whitespaces 
   keyNumber= $(this).val().trim();
//With an if condition I check initially the lenght of the string
//In this block of code if the above condition is true the first thing 
//is to set the class empty and the add the appropriate class.

Open in new window

The only thing that you have to do from this point is with someway to validate the PPR number from database or from an array object that you have already creates.This can be done via ajax or other method
Most Valuable Expert 2017
Distinguished Expert 2019
This one is on us!
(Get your first solution completely free - no credit card required)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.