I'm searching php validate functions. In my opinion regex is hard.
Has Php got ready function?
i want to validate this variable:
Telephone Numer
E-mail adress
Maybe More...
And i want to deactive html tags incoming data from textarea.
Take a look at Validate filters, filter_var.
For the HTML tags you can either remove them or escape them
Regular Expressions are going to be the most robust way to validate things like phone numbers and email addresses.
They're not too hard to learn if you have a good resource. And it's an excellent tool to have in your developer toolbox. Check out http://www.regular-expressions.info/tutorial.html. Otherwise, I'm sure you can find some existing validation functions by doing a search on Google or SO.
Take a look at strip_tags() for removing HTML tags from strings.
Take a look at filter_var
Validating telephone numbers and email addresses is very hard, and complicated but conflicting opinions as to what constitutes a valid phone number (UK only? Some other country only? Multiple countries? What about extensions? Should the number be prefixed with a +? Are hyphens or other separating characters allowed?) or email address (see one take on this).
PHP has no built ins for this.
Related
The following regex pattern (for PHP) is meant to validate any email address:
^[\w.-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,6}$
It says: "match at least one (or more) of upper- and/or lower-case letters, and/or periods, underscores and/or dashes followed by one and only one # followed by at least one (or more) of upper- and/or lower-case letters, and/or periods, and/or underscores followed by one and only one period followed by two to six upper- and/or lower-case letters.
This seems to match any email address I can think of. Still, this feeling of getting it right is probably deceptive. Can someone knowledgeable please point out an obvious or not-so-obvious vulnerability in this pattern that I'm not aware of, which would make it not perform the email validation the way it's meant to?
(To foresee a possible response, I'm aware that filter_var() function offers a more robust solution, but I'm specifically interested in the regular expression in this case.)
NOTE: this is a theoretical question about PHP flavor of regex, NOT a practical question about validating emails. I merely want to determine the limitations of what is reasonably possible with regex in this case.
Thank you in advance!
Using regular expression to validate emails is tricky
Try the following email as an input to your regex ie:^[\w.-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,6}$
abc#b...com
You can read more about email regex validation at http://www.regular-expressions.info/email.html
If you are doing this for an app then use email validation by sending an email to the address provided rather than using very complex regex.
The email address specification is pretty nuts. There are regexen out there that can do a full validation for it, but they are thousands of characters long. It may be better to parse it on your own, but PHP has a built in validator for email addresses:
filter_var($email, FILTER_VALIDATE_EMAIL);
EDIT:
In answer to your specific question of an email address that will fail, any that has the email name in quotes will because you don't account for them at all:
"explosion-pills"#aysites.com
Is it possible to display the strings that match a regular expression?
Example:
Take the expression /^AD\d{3}/
and display AD999
What I'm doing is validating a string that is pretty simple either containing all numbers, a few characters maybe, and maybe a '-'. I am validating a postal code on form submit against a database of all countries that use a postal code.
I could perform it in Javascript or PHP, if that makes any difference.
No. That sort of feature is not available.
You can try to implement it yourself, but I don't think that's the solution for you. Simply write the messages normally. Not everything must always be dynamic.
I like your way of thinking though.
It is possible. The developers of PEX figured it out.
Don't get your hopes up, I don't know of any javascript implementation.
There is one for javascript now: http://fent.github.io/randexp.js/.
I have understood your problem a little better from your additional comments.
Since your data is only postal codes, I suggest that it would possible to work in the other direction and store a picture in the database and automatically generate a regex from that.
For instance, UK postcodes look like AA?99? 9AA | AA?9A 9AA which is easily converted to a regex (using a regex!).
I am going through our old site files and data that has our members emails and correspondence for 10 years.
I am extracting all of the email addresses (and botched email entries) and adding them to our new sites db.
It was a beginner attempt cms and had no error checking and validation.
So, I am having trouble matching emails with spaces and double #.
jam # spa ces1.com
jam#spac es2.com
jam##doubleats.org
I have constructed this loose regex that intentionally allows for a whole bunch of incorrect email formats but, the above three are examples of ones I can't figure out.
Here is my current "working" code:
$pattern1= '([\s]*)([_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*([ ]+|)#([ ]+|)([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,}))([\s]*)';
$pattern2='\b[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\b';
$pattern="/$pattern1|$pattern2/i";
$isago = preg_match_all($pattern,$text,$matches);
if ($isago) {.......
I need another pattern that would allow the three email examples above to be recognized as email addresses. (actual validation comes later)
Also, is there is any other patterns I could use that would allow me to recognize possible emails in the files?
Thanks for any help.
For the third case you can change your # to #{1,2}.
For the first and second you can add a space in your regex pattern1:
$pattern1= '([\s]*)([_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*([ ]+|)#{1,2}([ ]+|)([ a-zA-Z0-9-]+\.)+([a-zA-Z]{2,}))([\s]*)';
$pattern2='\b[A-Z0-9._%+-]+#{1,2}[A-Z0-9.-]+\.[A-Z]{2,4}\b';
This answer is like a joke I know... but, how about this RegEx:
/[\S ]+#[\S ]+\.[\S ]+/i
That's works for you? I'm tested it in a document and match the three mails.
For general purpose you should use something like this:
/[A-Za-z0-9\._]+#[A-Za-z0-9\._]+\.[A-Za-z0-9\._]+/i
With that you would match all the emails, even separated by newline or commas.
I am trying to create 3 PHP regular expressions which do three things..
Gets emails e.g mr.jones#apple-land.com
Gets dates e.g 31/05/90 or 31-Jun-90
Gets nameservers e.g ns1.apple.co.uk
I have a big chunk of text and want to extract these things from it.
What I have so far is:
$regexp = '/[A-Za-z0-9\.]+[#]{1}[A-Za-z0-9\.]+[A-Za-z]{2,4}/i';
preg_match_all($regexp, $output, $email);
$regexp = '/[A-Za-z0-9\.]+[^#]{1}/i';
preg_match_all($regexp, $output, $nameservers);
$regexp = '/[0-9]{2,4}[-\/]{1}([A-Za-z]{3}|[0-9]{2})[-\/]{1}[0-9]{2,4}/i';
preg_match_all($regexp, $output, $dates);
Dates and emails work, but i dont know if that is an efficient way to do it..
Nameservers just dont work at all.. essentially I want to find any combinations of letters and numbers which have dots in between but not # symbols..
Any help would be greatly appreciated.
Thanks
RegEx's for emails are fairly complex. This is one place where frameworks shine. Most of the popular ones have validation components which you can use to solve these problems. I'm most familiar with ZendFramework validation, and Symfony2 and CakePHP also provide good solutions. Often these solutions are written to the appropriate RFC specification and include support for things that programmers often overlook, like the fact that + is valid in an email address. They also protect against common mistakes that programmers make. Currently, your email regex will allow an email address that looks like this: .#.qt, which is not valid.
Some may argue that using a framework to validate an email or hostname (which can have a - in it as well) is overkill. I feel it is worth it.
essentially I want to find any combinations of letters and numbers
which have dots in between but not # symbols..
regexp for finding all letters and numbers which have dots in between:
$regexp '/[A-Za-z0-9]{1,}(\.[A-Za-z0-9]{1,}){1,}/i'
Please note that you don't have to make it explicit you don't want '#' if what you are matching on doesn't include the #.
I would recommend using different patterns for your examples:
[\w\.-]+#\w+\.[a-zA-Z]{2,4} for emails.
\d{1,2}[/-][\da-zA-Z]{1,3}[/-]\d{2,4} for dates.
([a-zA-Z\d]+\.){2,3}[a-zA-Z\d]+ for namespaces.
Good luck ;)
For the nameservers i would suggest using: /[^.](\.[a-z_\d]+){3,}/i
I was wondering if the codes below are the correct way to check for a street address, email address, password, city and url using preg_match using regular expressions?
And if not how should I fix the preg_match code?
preg_match ('/^[A-Z0-9 \'.-]{1,255}$/i', $trimmed['address']) //street address
preg_match ('/^[\w.-]+#[\w.-]+\.[A-Za-z]{2,6}$/', $trimmed['email'] //email address
preg_match ('/^\w{4,20}$/', $trimmed['password']) //password
preg_match ('/^[A-Z \'.-]{1,255}$/i', $trimmed['city']) //city
preg_match("/^[a-zA-Z]+[:\/\/]+[A-Za-z0-9\-_]+\\.+[A-Za-z0-9\.\/%&=\?\-_]+$/i", $trimmed['url']) //url
Your street address: ^[A-Z0-9 \'.-]{1,255}$
you need not escape the single quote.
since you have a dot in the char
class, it will allow all char (except
newline). So effective your regex becomes ^.{1,255}$
you are allowing it to be of min
length of 1 and max of length 255. I
would suggest you to increase the min
length to something more than 1.
Your email regex: ^[\w.-]+#[\w.-]+\.[A-Za-z]{2,6}$
again you are having . in the char
class. fix that.
Your password regex: ^\w{4,20}$
allows for a passwd of length 4 to 20
and can contain only alphabets(upper
and lower), digits and underscore. I would suggest you to allow
special char too..to make your
password stronger.
Your city regex: ^[A-Z \'.-]{1,255}$
has . in char class
allows min length of 1 (if you want
to allow cities of 1 char length this
is fine).
EDIT:
Since you are very new to regex, spend some time on Regular-Expressions.info
This seems overly complicated to me. In particular I can see a few things that won't work:
Your regex will fail for cities with non-ASCII letters in their names, such as "Malmö" or 서울, etc.
Your password validator doesn't allow for spaces in the password (which is useful for entering pass-phrases) it doesn't even allow digits or punctuation, which many people will like to put in their passwords for added security.
You address validator won't allow for people who live in apartments (12/345 Foo St)
(this is assuming you meant "\." instead of "." since "." matches anything)
And so on. In general, I think over-reliance on regular expressions for validation is not a good thing. You're probably better off allowing anything for those fields and just validating them some other way.
For example, with email addresses: just because an address is valid according to the RFC standard doesn't mean you'll actually be able to send email to it (or that it's the correct email address for the person). The only reliable way to validate an email address is to actually send an email to it and get the person to click on a link or something.
Same thing with URLs: just because it's valid according to the standard doesn't actually mean there's a web page there. You can validate the URL by trying to do an actual request to fetch the page.
But my personal preference would be to just do the absolute minimum verification possible, and leave it at that. Let people edit their profile (or whatever it is you're verifying) in case they make a mistake.
There's not really a 'correct' way to check for any of those things. It depends on what exactly your requirements are.
For e-mail addresses and URLs, I'd recommend using filter_var instead of regexps - just pass it FILTER_VALIDATE_EMAIL or FILTER_VALIDATE_URL.
With the other regexps, you need to make sure you escape . inside character classes (otherwise it'll allow everything), and you might want to consider that the City/Street ones would allow rubbish such as ''''', or just whitespace.
Please don't assume that you know how an address is made up. There are thousands of cities, towns and villages with characters like & and those from other alphabets.
Just DON'T try to validate an address unless you do it thru an API specific to a country (USPS for the US, for example).
And why would you want to limit the characters in a users password? Don't have ANY requirements on the password except for it existing.
Your site will be unusable if you use those regex.