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:
嗨,我有一个表单,我试图验证底部的textarea。我想要它,所以如果默认值存在或如果框是空的,则会出错。这是我正在查看的表单的一部分:
<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:
这是我的PHP页面的一部分:
$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?"
如果textarea是空白的(因为我有正则表达式“。”所以他们可以使用键盘上的任何东西作为他们的评论),这可以正确地通过错误工作。但是如果它仍然说默认值“例如”我也希望它出错我们有5个人希望在上面的日期雇用大篷车7晚。我们能够烧烤吗?“
How can I do this?
我怎样才能做到这一点?
Kind regards
亲切的问候
4 个解决方案
#1
1
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):
如果只在非IE浏览器中显示文本,则还可以使用HTML5占位符属性(演示):
<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>
#2
1
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.
对于奖励积分,使用$ default变量填充您的模板,因此您没有在两个地方写入它。
#3
1
First, the eregi function is deprecated since php 5.3. preg_match is the way to go!
首先,自php 5.3以来,不推荐使用eregi函数。 preg_match是要走的路!
Second, if you want to test if no value was entered, why not compare to...nothing? php will treat empty strings as falsy values.
其次,如果你想测试是否输入了值,为什么不比较......什么都没有? php会将空字符串视为虚假值。
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.
看看php关于布尔值的文档。
#4
0
I might be misunderstanding. But if you want to validate on the server side (email.php) you can just do:
我可能会误解。但是如果你想在服务器端验证(email.php),你可以这样做:
$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 />';
#1
1
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):
如果只在非IE浏览器中显示文本,则还可以使用HTML5占位符属性(演示):
<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>
#2
1
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.
对于奖励积分,使用$ default变量填充您的模板,因此您没有在两个地方写入它。
#3
1
First, the eregi function is deprecated since php 5.3. preg_match is the way to go!
首先,自php 5.3以来,不推荐使用eregi函数。 preg_match是要走的路!
Second, if you want to test if no value was entered, why not compare to...nothing? php will treat empty strings as falsy values.
其次,如果你想测试是否输入了值,为什么不比较......什么都没有? php会将空字符串视为虚假值。
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.
看看php关于布尔值的文档。
#4
0
I might be misunderstanding. But if you want to validate on the server side (email.php) you can just do:
我可能会误解。但是如果你想在服务器端验证(email.php),你可以这样做:
$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 />';