473,624 Members | 2,258 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trim in javascript

164 New Member
Hi all

Am creating a form that contains several fields like name, phone, email etc.,

Users have to fill up the forms without leaving blank to those fields..

I have given script not to leave a field empty...


But some people giving spaces to those fields and giving submit..

So i have to avoid this problem...

Help me to do this..

Thanx all
Jun 29 '07 #1
3 1539
gits
5,390 Recognized Expert Moderator Expert
hi ...

have a look at the following example:

Expand|Select|Wrap|Line Numbers
  1. // input in field
  2. var j = '      r t      '; 
  3.  
  4. // match with regEx and replace spaces
  5. j = j.replace(/(^\s+|\s+$)/g, ''); 
  6.  
  7. // j == 'r t' now ;)
  8. alert(j == '');
  9.  
the regEx matches all blanks at begin and end of the string that has to be checked ... we replace them and the alert shows you the boolean-value of your empty-condition

kind regards ...
Jun 29 '07 #2
arunbalait
164 New Member
Ya thanx

I shall check it out...

Is there any other method...

And I dont want using functions
Jun 29 '07 #3
gits
5,390 Recognized Expert Moderator Expert
Is there any other method...
hmmm ... you may compare the value to 0:

Expand|Select|Wrap|Line Numbers
  1. // gives you a true ;) but its ugly and the value is not trimmed
  2. alert('             '  ==  0);
  3.  
  4. // gives you a false
  5. alert('          f  '  ==  0);
  6.  
And I dont want using functions
Why not?

kind regards ...
Jun 29 '07 #4

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

Similar topics

11
4730
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
1737
by: mhk | last post by:
Hi, i used Trim(my_val) and its giving error in JavaScript. is it a correct way to trim. Please suggest me, thanks in advance.
5
33299
by: Alex Vassiliev | last post by:
Hi all. Just wanted to share two handy RegEx expressions to strips leading and trailing white-space from a string, and to replace all repeated spaces, newlines and tabs with a single space. * JavaScript example: String.prototype.trim = function() { // Strip leading and trailing white-space
12
47653
by: Robert Mark Bram | last post by:
Hi All, I am using the following trim function: function trim (str) { return str.replace(/^\s*/g, '').replace(/\s*$/g, ''); } The problem is that this doesn't trim instances of the " " char - the non breaking space. Can this be represented in a grep statement at
3
4062
by: Shailesh Humbad | last post by:
I compiled this article on trim functions for Javascript: Javascript Trim LTrim and RTrim Functions http://www.somacon.com/p355.php If you have any comments, please let me know. Thanks, Shailesh
27
6834
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM? ----------------------------------------------------------------------- Using Regular Expressions (JavaScript 1.2/JScript 4+) : String.prototype.LTrim=new Function("return this.replace(/^\\s+/,'')") String.prototype.RTrim=new Function("return this.replace(/\\s+$/,'')") String.prototype.Trim= new...
7
2912
by: Henri | last post by:
Hello, any idea why a regexp-based trim function would work fine in firefox but not in ie? code is String.prototype.trim = function() { re = /\s*$/g; return this.replace(re, "");
4
5510
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I trim whitespace - trim/trimRight/trimLeft ----------------------------------------------------------------------- Using Regular Expressions (JavaScript 1.2/JScript 3+) : String.prototype.trimLeft = function() {
5
17581
by: aswath | last post by:
i used the below code for trimming.. var str = " blah blah "; var trimmed = str.replace(/^\s+|\s+$/g, '') ; alert(trimmed); alert(trimmed.value.length); its aint working.. i think small error.. can any1 retify.. thanks in advance..
0
8233
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
8474
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
7158
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
6108
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
5561
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2604
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
1
1784
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1482
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.