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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stripping White Space Problem

Status
Not open for further replies.

tnsbuff

Technical User
Jan 23, 2002
216
US
Hi,

I have a form that I have been running that is a combination of FrontPage javascript validation and a custom javascript that determines which cgi script will perform the action based on an option field.

That all works fine except that FrontPage validation doesn't take into consideration if someone places a space in a field; in other words even if someone just enters one space in a field, it passes the validation when it shouldn't.

I've tried including the following script that "Removes white space" (two versions, one for javascript v1.2), but the forms still go through so I'm obviously doing something wrong. Here's the strip white spaces code that I'm using (I've separated it from the rest of the code since I'm not sure where to place it. I tried calling the stripSpaces() function from the submit button's onClick event, but it doesn't seem to work.

All suggestions very much appreciated!

Code:
<SCRIPT LANGUAGE="JavaScript"><!--
function stripSpaces() {
    x = document.myForm.myText.value;
    while (x.substring(0,1) == ' ') x = x.substring(1);
    while (x.substring(x.length-1,x.length) == ' ') x = x.substring(0,x.length-1);
    document.myForm.myText.value = x;
document.write (document.myForm.myText.value);
}
//--></SCRIPT>

<SCRIPT LANGUAGE="JavaScript1.2"><!--
function stripSpaces() {
    var x = document.myForm.myText.value;
    document.myForm.myText.value = (x.replace(/^\W+/,'')).replace(/\W+$/,'');
document.write (document.myForm.myText.value;
}
//--></SCRIPT>

This is the code that determines which cgi script will process the form: (If the person selects "female" then one script processes it and sends the results to the "female" page, and if they select "male" then a different script processes it and and sends the results to the "male" page. (Just a little background to make things a little clearer.)

Code:
<script>
function redirect(frm){
newurl=getRadioValue(frm.gender);
frm.action=newurl;
}

function getRadioValue(radioObject)
{
 var value=null;
  for(var i=0; i<radioObject.length; i++){
   if(radioObject[i].checked){
    value=radioObject[i].value;
    break;
   }
  }
 return value;
}
</script>

This is the FrontPage form validation:

Code:
<script Language="JavaScript"><!--
function FrontPage_Form1_Validator(theForm)
{

  if (theForm.name.value == "")
  {
    alert("Please enter a value for the \"name\" field.");
    theForm.name.focus();
    return (false);
  }

  var radioSelected = false;
  for (i = 0;  i < theForm.gender.length;  i++)
  {
    if (theForm.gender[i].checked)
        radioSelected = true;
  }
  if (!radioSelected)
  {
    alert("Please select one of the \"gender\" options.");
    return (false);
  }

  if (theForm.avail.value == "")
  {
    alert("Please enter a value for the \"availability\" field.");
    theForm.avail.focus();
    return (false);
  }
  return (true);
}
//--></script>

Now, the form's submit button looks like this:

Code:
<input type="Submit" onClick="redirect(this.form)" value="Post Info!">

...and the form's action tag looks like this:

Code:
<form action="/cgi-bin/Matchfinder/matchfinder_m.cgi" method=post onsubmit="return FrontPage_Form1_Validator(this)" name="FrontPage_Form1">

Can anyone help?

Thanks very much!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top