473,320 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

[REGEX VALIDATOR] Function to check a empty field or with only space .......

Hi.
I'm taking my first steps on regex I set up
this simple function to check if a form field is empty
or with only space.
var onlySpaceRegexp = /^\s*$/;
function isEmpty(val)
{

if (onlySpaceRegexp.test(val) || val == "")
{
return true;
}
else
{
return false;
}
}
alert(isEmpty(""));//TRUE
alert(isEmpty(" "));//TRUE
alert(isEmpty(" v "));//FALSE
I'm wandering is it the best way ?

Take care.
Bye ;)

Nov 23 '06 #1
4 3167
whisher wrote on 23 nov 2006 in comp.lang.javascript:
Hi.
I'm taking my first steps on regex I set up
this simple function to check if a form field is empty
or with only space.
var onlySpaceRegexp = /^\s*$/;
function isEmpty(val)
{

if (onlySpaceRegexp.test(val) || val == "")
{
return true;
}
else
{
return false;
}
}
alert(isEmpty(""));//TRUE
alert(isEmpty(" "));//TRUE
alert(isEmpty(" v "));//FALSE
I'm wandering is it the best way ?
There is no best way in programming.
That's why it is fun!

However:

1

the || val == "" is never used,
as an empty string is already detected by the test()

2

/^\s*$/.test(val)
["must be all white-space"]

is equivalent to:

!/\S/.test(val)
["not any non-white-space"]

3

your function can be written as:

function whiteSpaceOnly(val) {
return !/\S/.test(val)
}

[giving it a better name,
not testing for true/false to return another pair of the same]
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Nov 23 '06 #2

whisher wrote:
Hi.
I'm taking my first steps on regex I set up
this simple function to check if a form field is empty
or with only space.
var onlySpaceRegexp = /^\s*$/;
Pre-initialising a regexp is handy if you are going to use it more than
once, but for one-of cases, there isn't much point other than for style
or convention.
function isEmpty(val)
{

if (onlySpaceRegexp.test(val) || val == "")
The second test is completely covered by the first (which matches
strings consisting of only zero or more spaces), and therefore is
redundant.
{
return true;
}
else
There is no need for an 'else' after a conditional return, though maybe
is makes maintenance easier.
{
return false;
}
}
alert(isEmpty(""));//TRUE
alert(isEmpty(" "));//TRUE
alert(isEmpty(" v "));//FALSE
I'm wandering is it the best way ?
You might consider one of the following:

function isEmpty(val){
return /^\s*$/.test(val);
}

function isEmpty(val){
return !/\S/.test(val);
}

You might want to handle cases where val isn't a string, the following
is a start but what should be returned if val isn't a string?

function isEmpty(val){
if (typeof val == 'string'){
return /^\s*$/.test(val);
}
// val isn't as string, what now?
}

--
Rob

Nov 23 '06 #3
VK

whisher wrote:
Hi.
I'm taking my first steps on regex I set up
this simple function to check if a form field is empty
or with only space.
var onlySpaceRegexp = /^\s*$/;
function isEmpty(val)
{

if (onlySpaceRegexp.test(val) || val == "")
{
return true;
}
else
{
return false;
}
}
alert(isEmpty(""));//TRUE
alert(isEmpty(" "));//TRUE
alert(isEmpty(" v "));//FALSE
I'm wandering is it the best way ?
I usually put the question from the other end: "Is this form field not
empty?" (contains some alphanumeric characters). But your way is fine
too.

test() method returns either true (match found) or false: this way
there is no need to additionally wrap it into return true / return
false branches.

var re = /\S+/;

function isEmpty(val) {
return (!(re.test(val));
}

Nov 23 '06 #4

VK wrote:
whisher wrote:
Hi.
I'm taking my first steps on regex I set up
this simple function to check if a form field is empty
or with only space.
var onlySpaceRegexp = /^\s*$/;
function isEmpty(val)
{

if (onlySpaceRegexp.test(val) || val == "")
{
return true;
}
else
{
return false;
}
}
alert(isEmpty(""));//TRUE
alert(isEmpty(" "));//TRUE
alert(isEmpty(" v "));//FALSE
I'm wandering is it the best way ?

I usually put the question from the other end: "Is this form field not
empty?" (contains some alphanumeric characters). But your way is fine
too.

test() method returns either true (match found) or false: this way
there is no need to additionally wrap it into return true / return
false branches.

var re = /\S+/;

function isEmpty(val) {
return (!(re.test(val));
}
;) ;) ;)
Thanks a lot buddies for the enlightments
The fog is lifing ;)
Take care.
Bye.

Nov 24 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Marcel Brekelmans | last post by:
Hello, I seem to get an extra empty field in every 'mysql_fetch_array' command I issue. For example: I have a simple table 'tblName': ID Name 1 Jane 2 Joe 2 Doe
2
by: Paul Telco | last post by:
Hello, I'm a new user designing a simple database to retrieve pre-prepared docunents for printing. I have five tables, a form to design the documents, a form to customise and retrieve the...
2
by: Elhanan | last post by:
hi all.. i have the following string: 200850625~01~464~^^200850625~01~464~^^200850625~01~908~^^ which i will need to turn to a mutli-dimentional string array i used result.Split(new...
1
by: Oleg Ogurok | last post by:
Hi all, I want to use RegularExpressionValidator to enforce non-empty integer format in a TextBox. However, the validator doesn't give the error when the textbox is empty. For example, if...
3
by: Mad Scientist Jr | last post by:
i am trying to validate a field for a double, but not allow commas the regex specifies any number of whole number digits * no comma * an optional...
3
by: Joachim | last post by:
Hi I am a beginner in VB.NET, and have a problem with empty field in Access I have transfered a worksheet in Excel to Access table. Some of the cels are empty I use VB.NET program to acces...
1
by: Thief_ | last post by:
I'd like to check if a textbox contains text & symbols. Sometimes it will have just carriage returns and/or spaces, and lots of them, and I want to check if this is the case so that I can clear the...
3
by: jonosborne | last post by:
Hi guys, im a bit of a novice being thrown in at the deep end ! Using MS Access 97 i have a table with data that is updated once a day from an Excel spreadsheet. I need a way of identifying when data...
1
by: Shan Yang | last post by:
Hi, I am handling tab delimited txt files that have multiple fields. But some of the fields can be empty. So it will appear as <TAB><TAB> in the file rather than <TAB>something<TAB>. If I read in...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.