473,399 Members | 4,254 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,399 software developers and data experts.

Javascript validation with Null

I am unable to have the "null" value working for the validation of the
text field.
The code is able to validate if the sid value is having one,two,three
spaces....but not more..it is sloppy coding...but it works....any
pointers to improve the code.

sid = document.form1.StudentID.value;
if (sid.length == 0 || sid == " " || sid ==" " || sid ==" " || sid
==" ")
{
alert("Student ID is Missing " + sid);
return false;

But when I am using, the sid is accepting values. with spaces.

if (sid.length == 0 || sid == null)
{
alert("Student ID is Missing " + sid);
return false;
}

Jun 20 '06 #1
2 28076
kr******@gmail.com wrote:
I am unable to have the "null" value working for the validation
of the text field.
Not true, the - null - value is working exactly as programmed.
The code is able to validate if the sid value is having one,two,three
spaces....but not more..it is sloppy coding...but it works....any
pointers to improve the code.

sid = document.form1.StudentID.value;
if (sid.length == 0 || sid == " " || sid ==" " || sid ==" " || sid
==" ")
{
alert("Student ID is Missing " + sid);
return false;
Why is it acceptable for the user to enter 5 or more spaces?

This type of validation would usually be done with a regular
expression.
But when I am using, the sid is accepting values. with spaces.

if (sid.length == 0 || sid == null)

<snip>

The - null - value does not equal any string value, even a sting made
up of white space, or an empty string.

Richard.

Jun 20 '06 #2
Hi,

kr******@gmail.com wrote:
But when I am using, the sid is accepting values. with spaces.

if (sid.length == 0 || sid == null)


This will cause an error if sid is null. If sid is null, then trying to
access its length property will crash the script, thus the validation
will fail.

JavaScript (and other C-like languages) has a nice feature: If you use a
logical OR, and if the first term of the expression is true, then the
second term won't be evaluated. This is an optiization for speed
(because no unnecessary evaluation is made), but also allows such
constructs:

if ( sid == null || sid.length == 0 )

In that case, if sid is null, then sid.length is not evaluated so there
is no error. If sid is not null, then sid.length doesn't cause an error.

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch
Jun 20 '06 #3

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

Similar topics

72
by: Stephen Poley | last post by:
I have quite often (as have probably many of you) come across HTML forms with irritating bits of Javascript attached. The last straw on this particular camel's back was a large form I was asked to...
4
by: Ian Cox | last post by:
I have a web form that contains a Datagrid. This grid has a number of columns, one of which contains a text box and validator for that text box. Everything works fine, when I press the "Save"...
1
by: avp | last post by:
Hi, We have an ASP.NET 2.0 (C#) application that has a web form with a CheckBoxList control and a CustomValidator control. The CustomValidator control is used to validate that at least one...
3
by: Wayne Sepega | last post by:
I have inherited from System.Web.UI.WebControls and overrode the OnRender of my new button. In the on render I generate some JavaScript and add it as an attribute as follows: ...
1
by: MrMaxPower | last post by:
Hi, I have an Outlook form which has VB validation script running behind it. I need to launch this form from a webpage. When opening from an <a> anchor, it opens the form OK, but not does not...
7
by: karen987 | last post by:
The code below is for a checkbox, in a form on an asp page. I want to make it mandatory for the user to click it. The other validation statements work fine, this one causes a problem though. The...
3
by: daokfella | last post by:
It looks like this is now an embedded resource. So is it permanently hidden? In earlier versions of .Net, I made a simple change to the WebUiValidation.js so that the validation summary was...
1
by: fran7 | last post by:
hi, I have two javascript form validation scripts. Although the forms have different names, one of the scripts overrides the other and validates both. They have the same onsubmit, and one has a...
14
by: Mtek | last post by:
Hi, We have a form defined with buttons like this: <a class="save_menu" href="javascript:document.Detail_Screen.action = 'savedata.php?screen=EDIT';document.Detail_Screen.submit();">Update</...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.