[英]Regex_match for validationg string from selectbox in codeigniter
I have this code in my view: 我认为此代码如下:
echo form_label('State', 'state');
$options = array(
'No state' => '- Select state -',
'Alabama' => 'Alabama',
'Florida' => 'Florida',
'California' => 'California',
);
echo form_dropdown('state', $options);
echo form_error('state', '<div class="error">', '</div>');
And in my controller this: 在我的控制器中:
$this->form_validation->set_rules('state', 'State', 'required|regex_match[??????]');
if ($this->form_validation->run() == FALSE)
{
// VALIDATION ERROR
$this->load->view('page_registration');
}
else
{
// VALIDATION SUCCESS
....
....
....
My question is what to type inside regex_match instead of the question marks, so when everythng else instead of No state is selected it will succed. 我的问题是在regex_match里面键入什么而不是问号,所以当选择其他而不是No状态时,它将成功。 If you select No state, then the registration page reload and shows the error.
如果选择“无状态”,则会重新加载注册页面并显示错误。
I need the regular expression code between the square brackets for regex_match. 我需要在正则表达式之间的正则表达式代码regex_match。
Thanks in advance. 提前致谢。
Try: /^(?!(No state)).*$/
(I wrapped the regexp for pregmatch, sorry that i forgot earlier) 试试:
/^(?!(No state)).*$/
(我用了正则表达式包装了pregmatch,对不起我之前忘记了)
Translated : The string should not begin with 'No state'. 翻译:字符串不应以“无状态”开头。 ( so 'No state my friend' would failed)
(因此“不说我的朋友”会失败)
Result: 结果:
preg_match('/^(?!(No state)).*$/', 'No state', $matches);
array()
preg_match('/^(?!(No state)).*$/', 'Alabama', $matches);
array ( 0 => 'Alabama' )
Regex are usefull so try to learn the syntax (google for a tutorial, there is plenty availables) 正则表达式很有用,因此请尝试学习语法(Google提供了教程,有很多可用的方法)
Why dont you put a Zero in the array 为什么不在数组中放入零
$options = array(
'0' => '- Select state -',
'Alabama' => 'Alabama',
'Florida' => 'Florida',
'California' => 'California',
);
And then in your controller you might use 然后在您的控制器中,您可以使用
$this->form_validation->set_rules('state', 'State', 'required|alpha');
EDIT: 编辑:
If you want to stick with regex and validate NOT ALPHA you can use something like this 如果您要坚持使用正则表达式并验证NOT ALPHA,则可以使用类似这样的内容
$this->form_validation->set_rules('state', 'state', 'trim|required|regex_match[/^[a-zA-Z]$/]');
Hope it helps 希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.