Click to See Complete Forum and Search --> : case statements


florida
03-28-2003, 01:28 PM
I have alot of these "if" statements ( I listed 2 as an example) and would like to put them in a case statement. My ifs work great for checking blank spaces in my form but now I would like to make it all into case statements.


function stdRequire()
{

stdInfo = ''
ct = 0


if(/^\s*$/.test(document.stand.poc_fnm.value))
{
stdInfo += "\npoc first name";
ct++;
}

if(/^\s*$/.test(document.stand.poc_lnm.value))
{
stdInfo += "\npoc last name";
ct++;
}

etc...

if (stdInfo != '')
{

alert(stdInfo);
return false;
}


}



/////
//My attempt using CASE statements:
/////

function stdRequire()
{

stdInfo = ''
ct = 0

switch(n)
{
case(/^\s*$/.test(document.stand.poc_fnm.value):

stdInfo += "\nPOC's First Name";
ct++;
break;

case(/^\s*$/.test(document.stand.poc_lnm.value):

stdInfo += "\nPOC's Last Name";
ct++;
break;

etc...

Any advice on if this is correct??

angrytuna
03-28-2003, 01:48 PM
It looks to me like you haven't defined "n" yet that you're using in your switch statement. It looks like what you're trying to do is:

switch(/^\s*$/.test(document.stand.poc_fnm.value)){

case 'true':
//do stuff
break;

case 'false':
//do stuff
break;
}



The Regular Expression test method will return a boolean value. Your two cases should therefore be the possible values of that boolean value.
I'm not sure that you really need a case/switch statement for a two case scenario. Why are you changing what you already have?

~AT

Nedals
03-28-2003, 01:54 PM
I don't think you can do what you are asking

switch (expression) {
case label : statements; break;
case label : statements; break;
... default : statements;
}

The expression must evaluate to match the 'label'
Javascript case/switch is not your typical case statement.

Hope that helps (or hinders) a little.