Click here to Skip to main content
14,303,797 members
Rate this:
Please Sign up or sign in to vote.
See more:
This how i validate my required characters and the length

function submitChangej(){

  var firstname = /^[a-zA-Z-\s]{2,128}$/;
  var lastname = /^[a-zA-Z-\s]{2,128}$/;
  var inputlastName = document.getElementById("lastname"); 
  var inputfirstName = document.getElementById("firstname");
 var inputSubmit = document.getElementById("apply");
 var Container = document.getElementById('Agreement');
  //if((firstname.value.match(firstname)) || (lastname.value.match(lastname))
  if((inputfirstName.value.length < 2 || inputfirstName.value.length > 128 ) || ( inputlastName.value.length < 2 || inputlastName > 128 )){ = 'none';
	  = 'block';


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
on the fields that i validate.

I have commented other code, that didn't work the match one.

Your will be appreciated
Updated 3-Dec-15 21:48pm
yeleswarapu ramakrishna 4-Dec-15 4:30am
did you get the solution the code mentioned below by me is working for me so i hope it works for u as well
Rate this:
Please Sign up or sign in to vote.

Solution 1

I think your problem is that you are using the same name for your regular expression variables, and for your controls.

Have you tried to give both these groups different naming schemes?
iDoKin 4-Dec-15 4:19am
Yes, i changed them to regLastname and regFirsname
phil.o 4-Dec-15 4:31am
No you didn't. In the statement document.getElementById("lastname"), apparently you still have some controls that have the same name as your variables. You should not :)
iDoKin 4-Dec-15 5:50am
I will try different name, and see what i get.
Rate this:
Please Sign up or sign in to vote.

Solution 3

var firstname = /^[a-zA-Z-\s]{2,128}$/g;
var lastname = /^[a-zA-Z-\s]{2,128}$/g;
var inputlastName = document.getElementById("lastname").value;
var inputfirstName = document.getElementById("firstname").value;
//use this or
if (inputlastName.match(firstname) && inputfirstName.match(lastname))
{ = 'none';
//use this
if (inputlastName.match(firstname) || inputfirstName.match(lastname))
{ = 'none';
iDoKin 4-Dec-15 5:50am
This does not seem to be working.
yeleswarapu ramakrishna 4-Dec-15 6:39am
you said in your question match is not working in the above mentioned code the match is working
iDoKin 7-Dec-15 3:31am
I don't want to hard-code the name like you did in var inputlastName = "ramakrishna";
yeleswarapu ramakrishna 7-Dec-15 4:04am
i was just giving example of how to do it and the hard-code part do it yourself and don't complain of simple things cause you are not my employer
iDoKin 7-Dec-15 4:09am
No I was not complaining, but thanks.
yeleswarapu ramakrishna 7-Dec-15 4:18am
always welcome buddy.i have removed hard-code since you did'nt want it
iDoKin 7-Dec-15 5:08am
I made it work. Thanks hey.
yeleswarapu ramakrishna 7-Dec-15 5:28am
happy to know you solved it.happy coding
iDoKin 7-Dec-15 6:27am
How can i not allow space as first character in those fields?

iDoKin 7-Dec-15 6:30am
Space must only be between the words, currently i can be able to not accept space only as character when you submit. But the problem is when your enter first letter as a space, i don't want that. Space should be between the words only.
yeleswarapu ramakrishna 7-Dec-15 6:33am
You can use myString.substring(0,1) or myString[0] to get the first character.

You can trim the input; this doesn't check whether or not it starts with a space, it makes such a check redundant and gives you usable input regardless of whether or not the original string had an initial space.

You could also match the string for /^\s/ which means an initial whitespace, or /^\s+/ meaning any number of initial whitespace.
iDoKin 7-Dec-15 6:36am
So i will have var firstname = /^[a-zA-Z-/^\s+/]{2,128}$/g; ?
iDoKin 7-Dec-15 6:44am
But this gave me error
yeleswarapu ramakrishna 7-Dec-15 6:45am
i think u better use trim in javascript on blur and assign the value gain to the firstname field
yeleswarapu ramakrishna 7-Dec-15 6:44am
it's my office time i am already late so i will answer after i get back that is 11 hours from now sorry if its urgent.
iDoKin 7-Dec-15 6:45am
No problem.
iDoKin 7-Dec-15 8:13am
/^[a-zA-Z][a-zA-Z\s]{2,128}$/ solved my issue.
yeleswarapu ramakrishna 8-Dec-15 1:00am
iDoKin 8-Dec-15 3:14am
Thanks for your time

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100