•
•
•
•
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 402,018 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,368 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: 837 | Replies: 5
![]() |
•
•
Join Date: Nov 2006
Posts: 8
Reputation:
Rep Power: 0
Solved Threads: 0
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 .
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.
•
•
Join Date: Feb 2007
Location: London
Posts: 114
Reputation:
Rep Power: 2
Solved Threads: 8
javascript doesn't have a native trim function, however this way does work
javascript Syntax (Toggle Plain Text)
var trimmed = str.replace(/^\s+|\s+$/g, '');
•
•
Join Date: Feb 2007
Location: London
Posts: 114
Reputation:
Rep Power: 2
Solved Threads: 8
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.
Better yet, add a new function to the String object type by using the prototype property.
And then use the function trim as if it belonged to the String type.
javascript Syntax (Toggle Plain Text)
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); }
And then use the function trim as if it belonged to the String type.
javascript Syntax (Toggle Plain Text)
var str = " okay this works fine "; 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."
"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."
![]() |
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Previous Thread: DOM - Removing the space between paragraphes
- Next Thread: Mamu help meeeeeeeeeeeeeeeeeeeee!



Linear Mode