473,466 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to validate length and match required character?

10 New Member
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.  

5 1249
Dormilich
8,658 Recognized Expert Moderator Expert
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
KingIcode
10 New Member
I target very specific group of people.
Dec 4 '15 #3
Dormilich
8,658 Recognized Expert Moderator Expert
so can exclude even apostrophes?
Dec 4 '15 #4
KingIcode
10 New Member
Yes, I work according to what is required.
Dec 4 '15 #5
KingIcode
10 New Member
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

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

Similar topics

3
by: bdwise | last post by:
I have this in my body tag: something();something(); document.thisForm.textBox1.focus();something(); And I want to find a part between the semicolons that ends in focus() and remove the...
0
by: Onder Kaya | last post by:
We have Windows NT 4.0 Oracle 8.1.7 and ORACLE ERP 11.5.8 We created new discrete job. This discrete job contains material requirements and we have custom form which is haz.issue. This form...
3
by: Stephen | last post by:
I have the following regular expression and at present it checks that a field contains only letters and is more than 2 characters in length. I would like to change this slightly to also check that...
5
by: jen_designs | last post by:
Is there a way to return the character position on a page? Not the x and y coordinates, but the number of characters on a page. For instance i have a html page with the following text: This is my...
0
by: hwait | last post by:
I've a string (got it via WebRequest): <td class=maintable noWrap bgcolor=#666666><b><font color=#ffffff>HDR A </font></b></td> .... Inner text A .... <td class=maintable noWrap...
25
by: ehabaziz2001 | last post by:
Why I can not begin my subscript of character arrrays with 0. In this program I can not do : do { na=getchar(); i++; na=getchar(); } while (na!='\n');
2
by: Jon Slaughter | last post by:
Can I use any reg exp(mainly for mod rewrite) where I can match any character but a fixed list? I need to match a string that does not contain any /'s but I don't want to have to use a character...
2
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post by:
How do I validate that there are xx number of bytes data after a given spot. Here is an example; 2008/04/24 19:16:50 ARES_EINDICATION 010.050.082.108 117.3.01 (1d61) RX 68 bytes 1D 61...
2
by: thj | last post by:
Hi. I've got this form that I'm trying to validate: <form id="periodForm" action="" method="post"> <p> Periode: <input id="startDate" name="startDate" type="text" size="7" value="<%=...
2
by: christopher taylor | last post by:
hello python-list! the other day, i was trying to match unicode character sequences that looked like this: \\uAD0X... my issue, is that the pattern i used was returning:
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.