Page 1 of 1

Function to check input form values

Posted: Sun Apr 08, 2007 12:05 pm
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

Posted: Sun Apr 08, 2007 12:22 pm
by powerful0x0
i think what you need is this...

Code: Select all

if (preg_match('/[a-zA-Z ]/', $content)) {
	
} else {
	
}

Posted: Sun Apr 08, 2007 12:33 pm
by powerful0x0
Sorry about the above function...

this is what you need exactly ...

Code: Select all

if (preg_match('/^[a-zA-Z ]*?$/', $content)) {
	
} else {
	
}

Posted: Sun Apr 08, 2007 12:37 pm
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) «$»

Posted: Sun Apr 08, 2007 1:53 pm
by crazytopu
So which character represents the space exactly?

Posted: Sun Apr 08, 2007 3:31 pm
by RobertGonzalez

Posted: Sun Apr 08, 2007 3:37 pm
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...

Posted: Sun Apr 08, 2007 6:05 pm
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.

Posted: Sun Apr 08, 2007 7:39 pm
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.

Posted: Sun Apr 08, 2007 8:21 pm
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

Posted: Mon Apr 09, 2007 10:23 am
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.

Posted: Mon Apr 09, 2007 10:57 am
by feyd
Just so people are aware, \s matches all whitespace characters. This includes tabs, carriage returns and so forth.

Posted: Mon Apr 09, 2007 11:11 am
by RobertGonzalez
I knew there was something special about that character.

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