473,666 Members | 2,357 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

trim spaces and check for empty form elements

25 New Member
I'm trying to very that the user actually entered something in the form, and not just spaces.

I guess the problem is in the first line of isBlank() function. I've tried the following:
  • elem.value.trim ();
  • elem.value=elem .value.trim();
  • elem.value=trim (elem.value);
  • elem.value.repl ace(/^\s+|\s+$/g,"");
and none works. It correctly gives me an error message if I leave the field completely blank.But if there's anything in the field, it does not trim away the spaces.

I found the trim functions somewhere on the web, so I'm guessing they are probably fine.

Here's the code:
Expand|Select|Wrap|Line Numbers
  1. function check_myForm(){
  2.     if(isBlank(myForm.ofc, 'Please enter ofc:')){
  3.         return false;
  4.     }
  5. }
  6.  
  7. function isBlank(elem, helperMsg) {
  8.     elem.value.replace(/^\s+|\s+$/g,"");
  9.     if(elem.value.length == 0){
  10.         alert(helperMsg);
  11.         elem.focus();
  12.         return true;
  13.     }
  14.     return false;
  15. }
  16.  
  17. String.prototype.trim = function() {
  18.     return this.replace(/^\s+|\s+$/g,"");
  19. }
  20. String.prototype.ltrim = function() {
  21.     return this.replace(/^\s+/,"");
  22. }
  23. String.prototype.rtrim = function() {
  24.     return this.replace(/\s+$/,"");
  25. }function ltrim(str) { 
  26.     for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
  27.     return str.substring(k, str.length);
  28. }
  29. function rtrim(str) {
  30.     for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
  31.     return str.substring(0,j+1);
  32. }
  33. function trim(str) {
  34.     return ltrim(rtrim(str));
  35. }
  36. function isWhitespace(charToCheck) {
  37.     var whitespaceChars = " \t\n\r\f";
  38.     return (whitespaceChars.indexOf(charToCheck) != -1);
  39. }
Sep 29 '08 #1
5 3516
acoder
16,027 Recognized Expert Moderator MVP
If you're trying to replace, you'd have to set the value of elem to the new value:
Expand|Select|Wrap|Line Numbers
  1. elem.value = elem.value.replace(...);
Alternatively, remove that line and just use the trim() method (that you've added to String):
Expand|Select|Wrap|Line Numbers
  1. if(elem.value.trim().length == 0){
Sep 29 '08 #2
Marjeta
25 New Member
Thanks for your response.

I tried your suggestion:
Expand|Select|Wrap|Line Numbers
  1. function isBlank(elem, helperMsg) {
  2.     if(elem.value.trim().length == 0){
  3.  
but it doesn't change the field.

Meanwhile I tried this one again, just for laughs:
Expand|Select|Wrap|Line Numbers
  1. function isBlank(elem, helperMsg) {
  2.     elem.value=trim(elem.value);
  3.     if(elem.value.length == 0){
  4.     ...
  5.  
and now it suddenly works exactly as I wanted it. Maybe I forgot a ; or something stupid like that when I first tried it.

But I'm still puzzled at why it doesn't work with string's member function.
Sep 29 '08 #3
acoder
16,027 Recognized Expert Moderator MVP
If you want to change the value, then you would have to set elem.value as you've done. trim() will work if you use it on the value:
Expand|Select|Wrap|Line Numbers
  1. elem.value = elem.value.trim();
Sep 29 '08 #4
Marjeta
25 New Member
Yup, that works.

Didn't work 2-3 hours ago - before I had my coffee. I guess lack of caffeine caused me to do some typos, such as forgetting a semicolon.

By the way, this was me very first JavaScript. Yaaay, I can do JavaScript now!!! Need to add that to my resume :)

Thanks for your help!!!!
Sep 29 '08 #5
acoder
16,027 Recognized Expert Moderator MVP
Well done! Since you're new to JavaScript, I would suggest you try going through some of the links in the Offsite Links sticky thread.
Sep 29 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
4272
by: Ruben | last post by:
Hello. I am trying to read a small text file using the readline statement. I can only read the first 2 records from the file. It stops at the blank lines or at lines with only spaces. I have a while statement checking for an empty string "" which I understand represents an EOF in Python. The text file has some blank lines with spaces and other with blanks. Thanks a lot.
4
11645
by: Jan Bols | last post by:
Whenever I execute TRIM(' ')in a stored procedure or trigger, I get an ORA-03113 error. I have an oracle db 8.1.7.0.1 Enterprise edition installed on a linux-Mandrake 9.1. EXEMPLE: I created the function IS_NULL which returns 1 or 0 if the parameter is empty or not:
22
12742
by: Simon | last post by:
Hi, I have written a function to trim char *, but I have been told that my way could be dangerous and that I should use memmove(...) instead. but I am not sure why my code could be 'dangerous' or even why there could be a problem. here is the code ////////
11
4733
by: Reply Via Newsgroup | last post by:
Folks, In PHP and some other scripting languages, one has trim() - It removes newline, tabs and blank spaces that might prefix, or suffix a string. Can someone tell me how I can do this in javascript? Much appreciated, randell d.
3
7016
by: Jon Kelling | last post by:
for all your javascript trimming needs... function trim(inputString) { if (typeof inputString != "string") return inputString; return inputString //clear leading spaces and empty lines .replace(/^(\s|\n|\r)*((.|\n|\r)*?)(\s|\n|\r)*$/g,"$2") //take consecutive spaces down to one .replace(/(\s(?!(\n|\r))(?=\s))+/g,"")
3
10688
by: Andy B | last post by:
I've tried using Trim or RTrim to strip trailing space characters from my data. When I check on the transformed data space characters are still there. We have an address table containing two fields: BuildName and RoadName. Both have the following properties: size 50, not indexed, not required, allowed zero length. Some records have BuildName, RoadName as null, some have content. No content is 50 chr long. When i run a Len(BuildName)...
22
9729
by: Terry Olsen | last post by:
I have an app that makes decisions based on string content. I need to make sure that a string does not contain only spaces or newlines. I am using the syntax 'Trim(String)" and it works fine. I thought I'd change it to the VB ..NET method "String.Trim" but that throws an object exception. Which brings the question: is it compliant to use Trim(String), or is it more within etiquette to use If Not String Is Nothing Then String.Trim?
3
3496
by: Pascal | last post by:
bonjour hello I would like to trim a string of all its white spaces so i used myString.trim() but it doesn't work as supposed : unsecable space are remaining in the middle of my string... i read in msdn : and notice that trim only Removes all occurrences of white space characters from the beginning and end of this instance. So what for the middle ? .NET Framework Class Library
121
5086
by: swengineer001 | last post by:
Just looking for a few eyes on this code other than my own. void TrimCString(char *str) { // Trim whitespace from beginning: size_t i = 0; size_t j; while(isspace(str)) {
0
8440
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8863
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8636
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7378
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6189
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4358
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2765
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1763
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.