By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,639 Members | 2,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,639 IT Pros & Developers. It's quick & easy.

How to validate length and match required character?

P: 10
This how i validate my required characters and the length

JavaScript

Expand|Select|Wrap|Line Numbers
  1. function submitChangej(){
  2.  
  3.   var inputlastName = document.getElementById("lastname"); 
  4.   var inputfirstName = document.getElementById("firstname"); 
  5.   var inputmobileNumber = document.getElementById("mobilenumber");
  6.   var inputidNumber = document.getElementById("idnumber");
  7.   var firstname =  /^[a-zA-Z-\s]{2,128}$/; 
  8.   var lastname = /^[a-zA-Z-\s]{2,128}$/; 
  9.   var mobilenumber = /^[0-9]{10,20}$/;
  10.   var idnumber = /^([0-9]){2}([0-1][0-9])([0-3][0-9])([0-9]){4}([0-1])([0-9]){2}?$/;
  11.  
  12.  
  13.   var inputSubmit = document.getElementById("applyforreseller");
  14.  
  15.   var Container = document.getElementById('Agreement');
  16.  
  17.   if((inputfirstName.value.length < 2 || inputfirstName.value.length > 128 ) || ( inputlastName.value.length < 2 || inputlastName > 128 ) || (inputmobileNumber.value.length < 10 || inputmobileNumber > 20) || (inputidNumber.value.length < 13)){
  18.  
  19.  
  20.         Container.style.display = 'none';
  21.   }
  22.   else{
  23.  
  24.         Container.style.display = 'block';
  25.   }
  26.   var Container = document.getElementById('Agreement');
  27.  if((firstname.value.match(firstname) != null) || (lastname.match(lastname) != null) || (mobilenumber.value.match(mobilenumber) != null) || (idnumber.value.value.match(idnumber) != null)){
  28.  
  29.         Container.style.display = 'none';
  30.   }
  31.   else{
  32.  
  33.         Container.style.display = 'block';
  34.  
  35.   } 
  36. }
  37.  

I want to hide the button if value does not match the required type, I m able to hide it on the length, but my match does not work.

I than call my method

Expand|Select|Wrap|Line Numbers
  1. submitChangej()
on the fields that i validate.


The button should be hidden when match and length are invalid, if everything is correct than the button should appear.

Your will be appreciated
Dec 4 '15 #1

✓ answered by KingIcode

Fixed my issue by doing the following


Expand|Select|Wrap|Line Numbers
  1. if(!firstname.test(inputfirstName.value) || !lastname.test(inputlastName.value) || !mobilenumber.test(inputmobileNumber.value) || !idnumber.test(inputidNumber.value)){
  2.  
  3.         if (Container){Container.style.display = 'none';}
  4.  }
  5.   else{
  6.         if (Container) {Container.style.display = 'block';}
  7.       } 
  8.  

Share this Question
Share on Google+
5 Replies


Dormilich
Expert Mod 5K+
P: 8,639
some comments first.
  • unless you target a very specific group of people you shouldn’t make any constraints on names (currently you exclude any people that have non-ASCII characters in their name, such as O’Brian, François, Jürgen, etc.)
  • there is an unescaped hypen in the character class definition. might yield unexpected results.
  • [0-9] equals \d
  • you can omit all parentheses in the ID number check and could compact it to /^\d{2}[01]\d[0-3]\d{5}[0-1]\d{2}?$/
Dec 4 '15 #2

P: 10
I target very specific group of people.
Dec 4 '15 #3

Dormilich
Expert Mod 5K+
P: 8,639
so can exclude even apostrophes?
Dec 4 '15 #4

P: 10
Yes, I work according to what is required.
Dec 4 '15 #5

P: 10
Fixed my issue by doing the following


Expand|Select|Wrap|Line Numbers
  1. if(!firstname.test(inputfirstName.value) || !lastname.test(inputlastName.value) || !mobilenumber.test(inputmobileNumber.value) || !idnumber.test(inputidNumber.value)){
  2.  
  3.         if (Container){Container.style.display = 'none';}
  4.  }
  5.   else{
  6.         if (Container) {Container.style.display = 'block';}
  7.       } 
  8.  
Dec 7 '15 #6

Post your reply

Sign in to post your reply or Sign up for a free account.