Click here to Skip to main content
15,911,711 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,


I have a jsp which displays the Add Department Field.Here my problem is if it is empty and click on search then it is displaying the alert message as please enter department name.It should not allow the duplicate values.
And my problem is if i give the Department Name as total space then also it is taking and placing that empty value in DB.But my requirement is when i give the department name as all spaces then also it has to display the message as please enter the department name.The code i am using is as below.


JavaScript
if(sub_dept_name == '' || sub_dept_name.trim() == '')
{
     alert("please enter sub department name");
     document.getElementById('txt_sub_deptname').focus();
     return false;
}

this code i wrote in .js file

can any one help please.
Posted
Updated 12-Sep-11 1:40am
v2
Comments
rkthiyagarajan 12-Sep-11 10:15am    
Whats your Real problem space key or string Length ?

Use the appropriate method (IsEmpty()) of the String[^] class.
Use the length[^] property of string.
 
Share this answer
 
v2
Comments
komali Guntur 12-Sep-11 7:52am    
Hi can you tell me hot to use that length[^] in my code i have the code as follows.


var sub_dept_name = document.getElementById('txt_sub_deptname').value;

if(sub_dept_name == '')
{
alert("please enter sub department name");
document.getElementById('txt_sub_deptname').focus();
return false;
}


i want the same validation for when i enter the space(continuously) also
Richard MacCutchan 12-Sep-11 8:19am    
Go to the link I posted for you and read the documentation.
rkthiyagarajan 12-Sep-11 8:17am    
I think you try to use one more java script like allowed only string.
Richard MacCutchan 12-Sep-11 8:20am    
What?
try this one...
JavaScript
sub_dept_name = trim(sub_dept_name);
if(sub_dept_name == null || sub_dept_name.length == 0)
{
     alert("please enter sub department name");
     document.getElementById('txt_sub_deptname').focus();
     return false;
}

function trim(s) {
	if(s != null && s.length > 0) {
		while (s.substring(0,1) == ' ') {
			s = s.substring(1,s.length);
		}
		while (s.substring(s.length-1,s.length) == ' ') {
			s = s.substring(0,s.length-1);
		}
		if(s.search(/[\S]/) == -1) {
			s="";
		}
	}
	return s;
}

Thanks & Regards,
Niral Soni
 
Share this answer
 
v2

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