Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript validation and "null" 2

Status
Not open for further replies.

bgreenhouse

Technical User
Feb 20, 2000
231
CA
Hi everyone

My latest problem is this. I have made a form with pull-down menus, and given the initial option (--) the value "null". When I write the following script to vaildate the form, it works in the browser preview of HomeSite, but not in Netscape (I don't think I've checked it in Explorer. Any suggestins as to why?

function processForm5() {
form5 = document.forms.questions4

if(form5.purchases.value =="null")
{
alert("Please let us know how many times you made a purchase on-line last year");
return;
}
else if(form5.amountspent.value =="null")
{
alert("Please let us know approximately how much you spent on-line last year");
return;
}

and then on to submit....

Ben
 
just so you know, Homesite's default browser is IE. Secondly, "null" is different than null. If you want to test and see if there is nothing in the field use this instead:

function processForm5() {
form5 = document.forms.questions4
allspace = /^\s*$/
if(allspace.test(form5.purchases.value))
{
alert("Please let us know how many times you made a purchase on-line last year");
return;
}
else if(allspace.test(form5.amountspent.value))
{
alert("Please let us know approximately how much you spent on-line last year");
return;
}

allspace above is a regular expression that will test to see if the string is blank or all spaces jared@aauser.com
 
Hey Jared

Thanks for the input, but what I'm actually trying to do is see if the value of the selection is null. I've made a select box like this (for example, I don't think this is the one in the script...)

<select name=&quot;purchases&quot;>
<option value=&quot;null&quot;>--</option>
<option value=&quot;0&quot;>0</option>
<option value=&quot;1-3&quot;>1-3</option>
<option value=&quot;4-10&quot;>4-10</option>
<option value=&quot;11-20&quot;>11-20</option>
<option value=&quot;20+&quot;>20+</option>
</select>

So I actually want to see if the value of the purchases field is &quot;null&quot;. It seems to work in IE (I guess - Homesite), but not Netscape...

Ben
 
maybe netscape is getting confused by the &quot;null&quot; string - jsut make the value an empty string and you can test against that &quot;&quot; - or if you think you might want to allow an empty string, make it something like: X(((---)))X
you know, just something you probably would never use jared@aauser.com
 
Ben,

String equality in Javascript.

var myStr = new String(&quot;hello&quot;);
var myStrX = new String(&quot;hello&quot;);

myStrX is not equal to myStr because they are different objects and that is what is compared when you do:

if ( myStr == myStrX ) or even
if ( myStr == &quot;hello&quot;)

So you can do this:

if(form5.purchases.value.indexOf(&quot;null&quot;) == 0)
// but of course the string &quot;nullJunk&quot; would evaluate to true!

So you could do this:

if(form5.purchases.value.indexOf(&quot;null&quot;) == 0) &&
form5.purchases.value.length == &quot;null&quot;.length )

Or my favorite:

function jstr_equals( instr){
return (this.indexOf(instr) == 0) &&
this.length == instr.length;
}
String.prototype.equals = jstr_equals;

if ( &quot;null&quot;.equals( form5.purchases.value))
// they are equal

Good luck
-pete
 
pete's favorite is great !
if the select field is not dynamically built, then jaredn's idea is smart ! or you can also try &quot;if (selectedIndex==0)&quot; (that means, &quot;if the user selected the 1st value&quot;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top