User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 391,556 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,664 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 825 | Replies: 5
Reply
Join Date: Nov 2006
Posts: 8
Reputation: amtallah is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
amtallah amtallah is offline Offline
Newbie Poster

Question help me please

  #1  
May 3rd, 2007
hi,

please i want to make function java script that check if user enter empty string((" ") more one space) or no if yes prevent this string from submit to database (this string will enter to database)

i know there are function trim() can remove empty string from text but not work with me .
Last edited by amtallah : May 3rd, 2007 at 5:54 am.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2007
Location: London
Posts: 114
Reputation: davidcairns is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 8
davidcairns davidcairns is offline Offline
Junior Poster

Re: help me please

  #2  
May 3rd, 2007
javascript doesn't have a native trim function, however this way does work

  1. var trimmed = str.replace(/^\s+|\s+$/g, '');
Reply With Quote  
Join Date: Nov 2006
Posts: 8
Reputation: amtallah is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
amtallah amtallah is offline Offline
Newbie Poster

Re: help me please

  #3  
May 3rd, 2007
hi
please can you explain this syntax str.replace(/^\s+|\s+$/g, '')
Reply With Quote  
Join Date: Feb 2007
Location: London
Posts: 114
Reputation: davidcairns is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 8
davidcairns davidcairns is offline Offline
Junior Poster

Re: help me please

  #4  
May 3rd, 2007
Str is you string variable, a string is an object with a number of properties and methods, one of which is replace. The messy looking first parameter to replace is a regular expression in javascript which in essence looks for spaces both at the start and end of the string but not in the middle. The second parameter ('') is the value to replace the found characters with. Hence we are replaceing all the found spaces at the start and end with blank strings, thus deleting them from the string.
Reply With Quote  
Join Date: Jan 2007
Posts: 2,510
Reputation: MidiMagic is on a distinguished road 
Rep Power: 7
Solved Threads: 103
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Posting Maven

Re: help me please

  #5  
May 5th, 2007
Look up "regular expressions" to understand this.
Daylight-saving time uses more gasoline
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,774
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 330
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: help me please

  #6  
May 6th, 2007
Better yet, add a new function to the String object type by using the prototype property.

  1. String.prototype.trim = function()
  2. {
  3. return this.replace(/^\s+|\s+$/g, "");
  4. }

And then use the function trim as if it belonged to the String type.

  1. var str = " okay this works fine ";
  2. alert(str.trim());
Last edited by ~s.o.s~ : May 6th, 2007 at 10:01 pm.
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb JavaScript / DHTML / AJAX Marketplace
Thread Tools Display Modes

Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 9:40 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC