Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Regular Expression to limit the characters length of field

Posted on 2006-05-23
7
Medium Priority
?
783 Views
Last Modified: 2013-12-03
HI,
I'm writing a RE to limit the field length must between 2characters to 30characters.

Here is the failed RE:
function check_field2($pat_name){
if(preg_match("/[A-Za-z0-9._-]{2,30}/",$pat_name)) return TRUE;
else return FALSE;
}

if(!check_field2($pat_name)){
echo '<b><font size="4">Patient Name : The patient name is out of character range </font></b><br>';
$error++;
}
0
Comment
Question by:Silmeria
  • 3
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
soapergem earned 600 total points
ID: 16748471
... So what exactly are you asking for? But as far as your regex goes, it wouldn't work correctly. It would ensure that someone enters at least 2 characters, sure, but they're free to enter more than 30 as you have it. You should explicitly tell it that you want only that expression by using the special characters for the beginning (^) and end ($) of the string. So use this:

function check_field2($pat_name)
{
    return preg_match('/^[A-Za-z0-9._\-]{2,30}$/', $pat_name) ? true : false;
}
0
 

Author Comment

by:Silmeria
ID: 16748668
Hi soapergem,
Sorry for my poor descrtiption.
Actually,I've a form that will insert to database.The data type of the field in database are char(30).
So,i wish to perform the data validation before it insert to database.
If user inputed more than 30characters at the form,error will shown and exit.

I'd try yours RE, either i input a valid or invalid string, this message "Patient Name : The patient name is out of character range" will shown.
It never insert to database whatever i input a valid string into the form.

Here is part of the code:
<?php
if ($REQUEST_METHOD == "POST")
{

function check_field1($pat_id){
if(preg_match("/^[123][0-9][0-9][0-9][0-9][0-9][0-9]$/",$pat_id)) return TRUE;
else return FALSE;
}

function check_field2($pat_name){
if(preg_match('/^[A-Za-z0-9._\-]{2,30}$/', $pat_name)) return TRUE;
else return FALSE;
}

if(!check_field1($pat_id)){
echo '<b><font size="4">Patient ID : You inputed an illegal patient ID.</font></b><br>';
$error++;
}

if(!check_field2($pat_name)){
echo '<b><font size="4">Patient Name : The patient name is out of character range </font></b><br>';
$error++;
}

if($error == "0"){

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "formAdd")) {
  $insertSQL = sprintf("INSERT INTO patient (pat_id, pat_name, dob, hkid, sex, blood_group, tel_no, mobile, fax, address, email, reg_by) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['pat_id'], "int"),
                       GetSQLValueString($_POST['name'], "text"),
.
.
.
  mysql_select_db($database_dbconnection, $dbconnection);
  $Result1 = mysql_query($insertSQL, $dbconnection) or die(mysql_error());
    }
  }
}
?>

0
 

Author Comment

by:Silmeria
ID: 16748801
Oops!i found the fault.i miss to declare the $pat_name.
0
 
LVL 6

Expert Comment

by:soapergem
ID: 16751787
Why'd you give me a C? Did you read the rules? (You should probably never do that....)
0
 
LVL 6

Expert Comment

by:soapergem
ID: 16751972
In the future, asking clarifying questions before assigning such a poor grade. And read this section, please:
http://www.experts-exchange.com/help.jsp#hi73
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question