开发者

Using html forms and php for validation on blank textarea or if default value is present

开发者 https://www.devze.com 2023-02-25 18:52 出处:网络
Hi I have a form I am trying to validate the textarea at the bottom. I want it so if the default value is present or if the box is empty it errors. This is the part of my form i am looking at:

Hi I have a form I am trying to validate the textarea at the bottom. I want it so if the default value is present or if the box is empty it errors. This is the part of my form i am looking at:

<form id="form" method="post" action="email.php">    

<p><label style="width: 400px; float: left; height: 20px" for="comments">Your enquiry details?</label>
<textarea style="width: 400px; height: 84px" id="comments" rows="1" cols="1" 
          onfocus="if (this.value == this.defaultValue) { this.value = ''; }" 
          name="comments">
e.g. There are 5 of us looking to hire either caravan for 7 nights on the dates above. Are we able to have BBQ's?
</textarea> </p>

 All sections MUST be completed!<br />

<p class="submit">    
<button type="reset">Reset</button>    
<button name="send" type="submit">Submit</button> </p></fieldset> </form>

This is part of my PHP page:

$string_exp = ".";
 if(!eregi($string_exp,$comments)) {开发者_开发百科
    $error_message .= 'You did not leave a comment!.<br />';

This works correctly by erroring if the textarea is blank (as I have the regular expression "." so they can use any thing on the keyboard for their comments) but I also want it to error if it still says the default value of "e.g. There are 5 of us looking to hire either caravan for 7 nights on the dates above. Are we able to have BBQ's?"

How can I do this?

Kind regards


if(!$comments || $comments == 'There are 5 of us looking to hire either caravan for 7 nights on the dates above. Are we able to have BBQ's?')

If it's ok to have the text only in non-IE browsers you could also use the HTML5 placeholder attribute (demo):

<textarea ... placeholder="here are 5 of us looking to hire either caravan for 7 nights on the dates above. Are we able to have BBQ's?"></textarea>


Expand your check to include your current check, and also a string check.

$default = "e.g. There are 5 of us looking to hire either caravan for 7 nights on the dates above. Are we able to have BBQ's?"

if (($comments == "") || ($comments == $default))) {
   $error_message .= 'You did not leave a comment!.<br />';

For bonus points, use the $default variable to populate your template, so you don't have it written in two places.


First, the eregi function is deprecated since php 5.3. preg_match is the way to go!

Second, if you want to test if no value was entered, why not compare to...nothing? php will treat empty strings as falsy values.

Third, assuming you have the value, why not compare to it?

if (!$comments || $comments === 'my default value') {
    $error_message .= 'You did not leave a comment!.<br />';
}

Take a look at php's documentation on Boolean values.


I might be misunderstanding. But if you want to validate on the server side (email.php) you can just do:

$default_comment = "There are 5 of us looking to hire either caravan for 7 nights on the dates above. Are we able to have BBQ's?"

$comments = (string)$_POST['comments'];

if( empty($comments) || $comments == $default_comment )
    $error_message .= 'You did not leave a comment!.<br />';
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号