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=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: -
function check_myForm(){
-
if(isBlank(myForm.ofc, 'Please enter ofc:')){
-
return false;
-
}
-
}
-
-
function isBlank(elem, helperMsg) {
-
elem.value.replace(/^\s+|\s+$/g,"");
-
if(elem.value.length == 0){
-
alert(helperMsg);
-
elem.focus();
-
return true;
-
}
-
return false;
-
}
-
-
String.prototype.trim = function() {
-
return this.replace(/^\s+|\s+$/g,"");
-
}
-
String.prototype.ltrim = function() {
-
return this.replace(/^\s+/,"");
-
}
-
String.prototype.rtrim = function() {
-
return this.replace(/\s+$/,"");
-
}function ltrim(str) {
-
for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
-
return str.substring(k, str.length);
-
}
-
function rtrim(str) {
-
for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
-
return str.substring(0,j+1);
-
}
-
function trim(str) {
-
return ltrim(rtrim(str));
-
}
-
function isWhitespace(charToCheck) {
-
var whitespaceChars = " \t\n\r\f";
-
return (whitespaceChars.indexOf(charToCheck) != -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: - elem.value = elem.value.replace(...);
Alternatively, remove that line and just use the trim() method (that you've added to String): - if(elem.value.trim().length == 0){
Thanks for your response.
I tried your suggestion: -
function isBlank(elem, helperMsg) {
-
if(elem.value.trim().length == 0){
-
but it doesn't change the field.
Meanwhile I tried this one again, just for laughs: -
function isBlank(elem, helperMsg) {
-
elem.value=trim(elem.value);
-
if(elem.value.length == 0){
-
...
-
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.
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: - elem.value = elem.value.trim();
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!!!!
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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.
|
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:
|
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
////////
|
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.
|
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,"")
| |
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)...
|
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?
|
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
|
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))
{
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |