Reputation: 957
i am validate my textbox that only contain numbers and also it can be null or blank.but when i leave it blank or enter any number it always display error message "Passing Year Only Contain Numbers.".my code is
<?php
include "config.php";
function is_valid_fname($postg_year)
{
if ( !preg_match ("/^[0-9\s]+$/",$postg_year)) {
?>
<center><strong><font color="red">Passing Year Only Contain Numbers.</font></strong></center>
<?php
return false;
}
}
if (isset($_POST['submit'])) {
$postg_year=$_POST['postg_year'];
if(is_valid_fname($postg_year))
{
//my code
}
<form method="post">
<input type="text" name="postg_year" class="small_text" width="60">
<input type="submit" name="submit" value="submit">
</form>
}
Upvotes: 0
Views: 62
Reputation: 1
the difference being that "+" requires at least one character, while "star()" accepts even 0 characters.so you have to replace "+" instead of "star()".like,
if ( !preg_match ("/^[0-9\s]*$/",$postg_year)) {
Upvotes: 0
Reputation: 2807
your form should have the submit button with the name attribute, you are checking it in php if it is set but in the form you have not given it any name, so is_valid_fname()
will never be called in php, use the below code:
<input type="submit" name = "submit" value="submit">
Upvotes: 1
Reputation: 7662
Try
function is_valid_fname($postg_year)
{
if ( !preg_match ("/^[0-9\s]*$/",$postg_year))
{
return '<center><strong><font color="red">Passing Year Only Contain Numbers.</font></strong></center>';
}
else
return false;
}
Upvotes: 0
Reputation: 337
if I understand correctly... change this:
if ( !preg_match ("/^[0-9\s]+$/",$postg_year)) {
into this:
if ( !preg_match ("/^[0-9\s]*$/",$postg_year)) {
the difference being that "+" requires at least one character, while "*" accepts even 0 characters.
Upvotes: 2