Function to check input form values

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Function to check input form values

Post by crazytopu »

I am using this function to check if an input value is String, the only problem with this is if there is a space in the string it returns false.

How do I modify this so I can accept string with space as well?

Code: Select all

function isLetters($element) {
  return !preg_match ("/[^A-z]/", $element);
}
Many Thanks
powerful0x0
Forum Newbie
Posts: 7
Joined: Fri Apr 06, 2007 12:46 pm

Post by powerful0x0 »

i think what you need is this...

Code: Select all

if (preg_match('/[a-zA-Z ]/', $content)) {
	
} else {
	
}
powerful0x0
Forum Newbie
Posts: 7
Joined: Fri Apr 06, 2007 12:46 pm

Post by powerful0x0 »

Sorry about the above function...

this is what you need exactly ...

Code: Select all

if (preg_match('/^[a-zA-Z ]*?$/', $content)) {
	
} else {
	
}
Last edited by powerful0x0 on Sun Apr 08, 2007 12:47 pm, edited 1 time in total.
powerful0x0
Forum Newbie
Posts: 7
Joined: Fri Apr 06, 2007 12:46 pm

Post by powerful0x0 »

and here is explaination for the above regular expression:

^[a-zA-Z ]*?$

Assert position at the start of the string «^»
Match a single character present in the list below «[a-zA-Z ]*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
A character in the range between "a" and "z" «a-z»
A character in the range between "A" and "Z" «A-Z»
The character " " « »
Assert position at the end of the string (or before the line break at the end of the string, if any) «$»
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

So which character represents the space exactly?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

powerful0x0 wrote:Sorry about the above function...

this is what you need exactly ...

Code: Select all

if (preg_match('/^[a-zA-Z ]*?$/', $content)) {
	
} else {
	
}
If you read this thread: Holes in preg_match filters you may wonder if it's really what he needs...
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

Both ctype_alnum() and ctype_alpha() don't accept space as part of the string. So, I still didn't find a work around for my problem.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Will this work for you:

Code: Select all

if(ctype_print($foo) && !empty($foo)):

endif;
[A-Za-z ] * note the space should capture any letter and spaces.



If these are not solutions I think you'll need to be more specific.
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

okay, if you go here:

http://www.compquo.com/joinBuyer.php

or here

http://www.compquo.com/joinSupplier.php

and then type some values and hit submit you will see error message if you input inappropriate values.

Just for the name field, if you type one string without any space you don't get any error message but if you type two or more string with space in between them the error message it gives is "please type character value for name field".

So if you input "John" not a problem, but if you type "John Smith" you get that error massage. But apart from that, if you type other values like numeric or alpha numeric for the name field the function serves the purpose and gives you an error message.

I just need something that will say okay if the value is a string no matter if there is space or not.

The functions I have consulted none of them have got this built-in. I dont want to re invent the wheel so just looking around if someone already done it.

Cheers
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

crazytopu wrote:So which character represents the space exactly?
I think you can use "\s" as a space also, as long as it is inside the class.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Just so people are aware, \s matches all whitespace characters. This includes tabs, carriage returns and so forth.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I knew there was something special about that character.

Sorry for misleading anyone. I should have checked that before posting. Apologies.
Post Reply