Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Null Strings

Status
Not open for further replies.

GlynW

Programmer
Feb 14, 2001
22
GB
I write software in many languages, and am just getting to grips with Java and Java Script. I know all the programming concepts, but it's just finding the matching keywords.

I want to know how to tell if a string is empty:

I've tried:
if (request.company_type.length > 1){
which is fine unless they type a space. is there a command to trim the string of spaces before I check the length property?

I'm basicly trying check if some fields on a web page have something valid! entered. but not spaces or nulls

Cheers

Glyn


 
The String class has a method called trim() that removes leading and trailing spaces.
 
Hi,
Yes there is a method to trim Strings in java and that is
yourString = yourString.trim();

regards
PNS
 
As far as I can see the only way to ensure the string is non-empty and not null is
if(myString!=null&&myString.trim().length()!=0)
because of course if the string is null, the trim() will give a null pointer exception
 
Note: Java and Javascript are not the same. They are not even remotely similar. So it makes a big difference between wheather you are using Java or Javascript.

Secondly, your statement should look like:
Code:
String company_info = request.getParameter("company_type");
if (company_info != null && company_info.trim().equals("")) {
  /* COMPANY INFO ENTERED */
}
Wushutwist
 
GlynW (forgot to login Doh!)

wushutwist,
I know what you mean by Java and Javascript not being the same, but as a newbie to Java and Javascript, this is where I'm getting into trouble...

The really good example you gave, is that for Java or Javascript, as I was having problems using the .trim method

I've actually got round the problem... probably a bit long winded with the following:... Any comments on tidying..


function validatefields{
<!--- ****** CHECK COMPANY NAME ****** --->
if (!checkfield(request.company_name.value)){
alert(&quot;Company Name Not Entered&quot;);
request.company_name.focus();
return false;
}
}

<!--- CHECK IF FIELD IS NOT EMPTY OR JUST SPACES --->
function checkfield(fieldname){
if (fieldname==&quot;&quot;){
return false;
}else{
if (notspaces(fieldname)){
return true;
}
}
}

<!--- CHECK IF NOT JUST SPACES ENTERED INTO FIELD --->
function notspaces(tvar){
for (var i=0;i<tvar.length;i++){
tchar=tvar.substring(i,i+1)
if (tchar != &quot; &quot;){
return true;
}
}
}



Cheers for the help everyone

Glyn

 
I see. I assumed you were using JAVA. I would suggest that you post any further JAVASCRIPT questions to the Javascript group. You will most likely get a better and faster response. Wushutwist
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top