Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
This how i validate my required characters and the length

JavaScript
JavaScript
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 )){
 
 	
		Container.style.display = 'none';
  }
  else{
	  
        Container.style.display = 'block';
  }
  
}

</script>



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
JavaScript
submitChangej()
on the fields that i validate.

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

Your will be appreciated
Posted
Updated 3-Dec-15 21:48pm
v2
Comments
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

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?
 
Share this answer
 
Comments
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.
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))
{
Container.style.display = 'none';
}
//use this
if (inputlastName.match(firstname) || inputfirstName.match(lastname))
{
Container.style.display = 'none';
}
 
Share this answer
 
v5
Comments
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.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900