We help IT Professionals succeed at work.

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

x

javascript check input pattern and add class if correct pattern

movieprodw
movieprodw asked
on
256 Views
Last Modified: 2017-11-21
Hello,

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'

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

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
Comments inside the codes:
HTML:
<style>
	.nosnmatch { border: solid 2px blue; }
	.snmatch { border: solid 2px green;	}
</style>
<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" />
</form>

Open in new window


JQuery code:

$(document).ready(function(){
//Via JQuery I target the input element.I use keydown so
//to get and the backspace key function
  $('#check').keydown(function(e){
   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();
   keyLenght=keyNumber.length;    
//With an if condition I check initially the lenght of the string
   if(keyLenght===10){
     if(keyNumber==='PPR0000000'){
//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.
     $(this).attr('class','');
     $(this).addClass('snmatch');
     }
   }else{
     $(this).attr('class','');
     $(this).addClass('nosnmatch');
   } 
  });
  
});

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
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

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.

OR

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.