Hi every one i want to validate postal address
I require the text field should contains Alphabets in Upper and lower case, numbers, hyphen, fullstops forward and back slash and commas and spaces in it

currently i am using this fucntion but its not getting me the same thing which i require can anyone help me in this

if(eregi("[0-9]+\s(\w)*(\W)(\s?)(\w)*(\W)(#[0-9])?(\W*)(\w)*(\W)(\s?)(\w)*(\s?)(\w)*", $address)) {

    $valid_address = $address;      


} else {
    $error_address = 'Street address must be valid';
}  

Recommended Answers

All 4 Replies

I require the text field should contains Alphabets in Upper and lower case, numbers, hyphen, fullstops forward and back slash and commas and spaces in it

Try:

$regex = '/[A-Za-z0-9\-\\,.]+/';
$is_valid = preg_match($regex, $address); // Becomes true or false.

This does exactly what you ask. The following code is an example of validating a street name followed by a house number:

$regex = '/
    ^ # Start of string
    [a-z\- ]+ # Alphabetical chars, hyphens and spaces
    [ \t]* # Followed by one or multiple tabs or spaces
    \d # House number should start with at least one number
    [0-9a-z]* # House number may, for the rest, exist of numbers and/or letters
    $ # End of string
    /ix';
$is_valid = preg_match($regex, $address); // Returns true or false.

If you have an example address, I will be glad to help you write a regex that matches that address.

please check if i wrote this code right will it work ?

$regex = '/[A-Za-z0-9\-\\,.]+/';
if(preg_match($regex, $address)) {

    $valid_address = $address;      


} else {
    $error_address = 'Street address must be valid';
}  

Yes that should work :). It checks if the given input ($address) consists of only alphabetical and numerical characters and hyphens, backslashes, dots and comma's.

Oh wait. I forgot one thing. Replace your current $regex by the following:

$regex = '/^[A-Za-z0-9\-\\,.]+$/';

(In the original I forgot to add the ^ and $ at the start and end, to define that the whole string should match the given regex).

Member Avatar for iamthwee

Rather than entire the minefield of regex (especially if you're not too sure what you are doing) why not just create a character array and check that all the characters of the string entered passes the criteria.

A nested for loop would work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.