/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Form being submitted with blank space

I have a form that is built in html and then sent to a validation form in php. Everything works great with a pop up box if a field is left empty on the form. But some people have been putting in space bar spaces into the fields and the form still submits. How can I get the form to submit only if there are actual characters in it rather than allowing space bar spaces?

Is this a php issue or something I work into the html page?

Thank you for your help!!

to post a comment
PHP

15 Comments(s)

Copy linkTweet thisAlerts:
@stephan_gerlachSep 19.2007 — You probably have something like this somewhere

[code=php]

if (strlen($_POST['dd']) > 0 ) {

...

}

[/code]


what you want to do is to remove all spaces at the front and the end of the text

this is being done this way

[code=php]

if (strlen(trim($_POST['dd'])) > 0 ) {

...

}

[/code]


hope this helps
Copy linkTweet thisAlerts:
@TJ111Sep 19.2007 — Put this in your PHP code:
[code=php]
if (ereg('^s*$', $formVariable)) {
//what to do if only spaces
} else {
//what to do if not only spaces
}[/code]

That checks to see if the only item in the string is some sort of space.
Copy linkTweet thisAlerts:
@charliewonkanobauthorSep 19.2007 — I have taken over the website for someone else, so I didn't build this page. But this is the php that is in the form fields <input name='firstname' type='text' size='50' maxlength='50' value='<?PHP echo ($firstname) ?>' />

<?PHP if (!$firstname)

echo "&nbsp; First Name is Required.";

?>[/QUOTE]
Copy linkTweet thisAlerts:
@charliewonkanobauthorSep 19.2007 — Where would I put that snippet of code? if (ereg('^s*$', $formVariable)) {

//what to do if only spaces

} else {

//what to do if not only spaces

} [/QUOTE]
Copy linkTweet thisAlerts:
@TJ111Sep 19.2007 — I forgot to mention that the example I posted would also cause an empty field to not pass validation. I'm not entirely sure the way your form is layed out, but I think I got the idea. It would go like this.

[code=php]
<input name='firstname' type='text' size='50' maxlength='50' value='<?PHP echo ($firstname) ?>' />
<?PHP if (ereg('^s*$', $firstname)) {
echo "&nbsp; First Name is Required.";
}
?> [/code]
Copy linkTweet thisAlerts:
@charliewonkanobauthorSep 19.2007 — hmmm that didn't work. But it was a great idea. It still lets the form get submitted with space bar spaces instead of an actual first name.
Copy linkTweet thisAlerts:
@TJ111Sep 19.2007 — Well the part I put it in doesn't do any actual validation. What is the validation part of your script?
Copy linkTweet thisAlerts:
@charliewonkanobauthorSep 19.2007 — oh ?

Well in the first page it has this chunk of code:

<script type="text/JavaScript">

<!--

function MM_findObj(n, d) { //v4.01

var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {

d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}

if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];

for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);

if(!x && d.getElementById) x=d.getElementById(n); return x;

}



function MM_validateForm() { //v4.0

var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;

for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);

if (val) { nm=val.name; if ((val=val.value)!="") {

if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');

if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.n';

} else if (test!='R') { num = parseFloat(val);

if (isNaN(val)) errors+='- '+nm+' must contain a number.n';

if (test.indexOf('inRange') != -1) { p=test.indexOf(':');

min=test.substring(8,p); max=test.substring(p+1);

if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.n';

} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.n'; }

} if (errors) alert('The following error(s) occurred:n'+errors);

document.MM_returnValue = (errors == '');

}

//-->

</script>
[/QUOTE]




and then

<form name="RMAForm" id="RMAform" action="RMAValidation.php" method="post" onsubmit="MM_validateForm('firstname','','R','lastname','','R','street','','R','city','','R','zip','','R','ZIP','','NisNum','phone','','RisNum','email','','RisEmail','prodName','','R','prodCode','','R','ZIP','','RisNum','orderDate','','R','returnReason','','R');return document.MM_returnValue">[/QUOTE]



Then it goes to the validation form which looks like this:

<table>

<tr>



<td ALIGN="left" VALIGN="top" WIDTH="750"><?PHP
@extract($_POST);

?>

<table cellpadding='0' cellspacing='0' border='0' class='formTable'>

<tr>

<td colspan='3' class='header'><h2>RMA - Return Mechandise Authorization Form</h2></td>

</tr>

<tr>

<td colspan="2"><br>

Please verify that the below information is complete and accurate.<br></td>

</tr>

<td width="116"><form name='RMAForm' id='RMAform' action='RMAprocess.php' method='post' >

<tr>
<td colspan='2' class='formsectionHeading'>Request Type</td>
</tr>
<tr>
<?PHP

if ($requestType == 'exchange'){

?>

<td class='formLeftColumn'>Exchange:</td>

<td width='418' class='formRightColumn'><input name='requestType' type='radio' value='exchange' checked />

</td>

</tr>

<tr>

<td class='formLeftColumn'> Refund:</td>

<td class='formRightColumn'><input name='requestType' type='radio' value='refund' />

</td>

</tr>

<?PHP } elseif ($requestType == 'refund'){ ?>

<td class='formLeftColumn'>Exchange:</td>

<td width='418' class='formRightColumn'><input name='requestType' type='radio' value='exchange' />

</td>

</tr>

<tr>

<td class='formLeftColumn'> Refund:</td>

<td class='formRightColumn'><input name='requestType' type='radio' value='refund' checked />

</td>

</tr>

<?PHP } ?>

<tr>

<td colspan='2' class='formsectionHeading' > Contact Information</td>

</tr>

<tr>

<td class='formLeftColumn'> First Name:</td>

<td class='formRightColumn' ><input name='firstname' type='text' size='50' maxlength='50' value='<?PHP echo ($firstname) ?>' />

<?PHP if (ereg('^s*$', $firstname)) {

echo "&nbsp; First Name is Required.";

}

?>

</td>

</tr>...etc.[/QUOTE]


Thank you for your help and your patience with me.
Copy linkTweet thisAlerts:
@TJ111Sep 19.2007 — Hmm it looks like your form is being validated in javascript, not PHP. On top of that it's being validated by a premade javascript function, from the menumonster or menumachine (forget whatever its called) javascript library.

Oh on second glance looks like it also gets passed to a page called "RMAvalidation.php". That's the page where this would have to go. Post the script from that page and I can try and help you find where it goes.
Copy linkTweet thisAlerts:
@charliewonkanobauthorSep 19.2007 — The RMAvalidation page looks like this:

The Javascript:

[CODE]<script language="JavaScript" type="text/javascript">
<!--
function openNewWindow(URLtoOpen, windowName, windowFeatures) { newWindow=window.open(URLtoOpen, windowName, windowFeatures); }

function MM_swapImgRestore() { //v3.0
var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>
[/CODE]


The page:
[CODE]<table>
<tr>

<td ALIGN="left" VALIGN="top" WIDTH="750"><?PHP
@extract($_POST);
?>
<table cellpadding='0' cellspacing='0' border='0' class='formTable'>
<tr>
<td colspan='3' class='header'><h2>RMA - Return Mechandise Authorization Form</h2></td>
</tr>
<tr>
<td colspan="2"><br>
Please verify that the below information is complete and accurate.<br></td>
</tr>
<td width="116"><form name='RMAForm' id='RMAform' action='RMAprocess.php' method='post' >

<tr>
<td colspan='2' class='formsectionHeading'>Request Type</td>
</tr>
<tr>
<?PHP
if ($requestType == 'exchange'){
?>
<td class='formLeftColumn'>Exchange:</td>
<td width='418' class='formRightColumn'><input name='requestType' type='radio' value='exchange' checked />
</td>
</tr>
<tr>
<td class='formLeftColumn'> Refund:</td>
<td class='formRightColumn'><input name='requestType' type='radio' value='refund' />
</td>
</tr>
<?PHP } elseif ($requestType == 'refund'){ ?>
<td class='formLeftColumn'>Exchange:</td>
<td width='418' class='formRightColumn'><input name='requestType' type='radio' value='exchange' />
</td>
</tr>
<tr>
<td class='formLeftColumn'> Refund:</td>
<td class='formRightColumn'><input name='requestType' type='radio' value='refund' checked />
</td>
</tr>
<?PHP } ?>
<tr>
<td colspan='2' class='formsectionHeading' > Contact Information</td>
</tr>
<tr>
<td class='formLeftColumn'> First Name:</td>
<td class='formRightColumn' ><input name='firstname' type='text' size='50' maxlength='50' value='<?PHP echo ($firstname) ?>' />
<?PHP if (ereg('^s*$', $firstname)) {
echo "&nbsp; First Name is Required.";
}
?>
</td>
</tr>
<tr>
<td class='formLeftColumn'> Last Name:</td>
<td class='formRightColumn' ><input name='lastname' type='text' size='50' maxlength='50' value='<?PHP echo ($lastname)?>'>
<?PHP if (!$lastname)
echo "&nbsp; Last Name is Required.";
$error=1;
?>
</td>
</tr>
<tr>
<td class='formLeftColumn'> Organization:</td>
<td class='formRightColumn' ><input name='organization' type='text' size='50' maxlength='50' value='<?PHP echo($organization)?>'>
</td>
<tr>
<td class='formLeftColumn'> Street Address:</td>
<td class='formRightColumn' ><input name='street' type='text' size='50' maxlength='50' value='<?PHP echo($street)?>'>
<?PHP if (!$street)
echo "&nbsp; Street Address is Required.";
$error=1;
?>
</td>
<tr>
<td class='formLeftColumn'> City:</td>
<td class='formRightColumn' ><input name='city' type='text' size='25' maxlength='25' value='<?PHP echo($city)?>'>
<?PHP if (!$city)
echo "&nbsp; City is Required.";?>
</td>
</tr>....and more of the form code.[/CODE]


Thank you!!
Copy linkTweet thisAlerts:
@TJ111Sep 19.2007 — Oh ok I see now. This line wasn't included previously.
[code=php]
$error=1;[/code]

I'm [i]assuming[/i] that this is a binary operator for if there's a form error. The part of the script that contains a conditional with something like "if ($error == 1)" will tell you for sure if it is or not. This is how it should be:

[code=php]
Every place this occurs:
<?PHP if (!$lastname)
echo "&nbsp; Last Name is Required.";
$error=1;
?>
Replace it with this (change the variables to match):
<?PHP if (ereg('^s*$', $lastname) || !$lastname) {
echo "&nbsp; Last Name is Required.";
$error = 1;
}
?>[/code]
Copy linkTweet thisAlerts:
@charliewonkanobauthorSep 19.2007 — It still accepts the form with the spacebar spaces. bummer. Is there maybe something I should put on the first page before it even gets to the validation page? I need it to only move forward if the spaces have characters, none of this silly spacebar stuff. ?
Copy linkTweet thisAlerts:
@TJ111Sep 19.2007 — The problem with doing it client-side is if that the user has javascript disabled, it goes through anyway. After thinking about it, 'ereg(' probably isn't the ebst choice here, as we only need to know if that exists.
[code=php]
<?PHP if ((strpos('^s*$', $lastname)!==false) || (!$lastname)) {
echo "&nbsp; Last Name is Required.";
$error = 1;
}
?>[/code]
Copy linkTweet thisAlerts:
@charliewonkanobauthorSep 19.2007 — It still accepts space bar spaces as a form entry. So we get a form with a bunch of empty looking spaces. Is there a way to require that it have letters and/or numbers. Maybe specify a length? I notice that people just put two space bar spaces. Maybe if I could make it be at least 5 characters then they get a message to actually type a word into the box.
Copy linkTweet thisAlerts:
@charliewonkanobauthorSep 20.2007 — Got it figured out on the Javascript forum. Thanks for all of your help!!
×

Success!

Help @charliewonkanob spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...