0

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';
}  

Edited by hallianonline

3
Contributors
4
Replies
31
Views
4 Years
Discussion Span
Last Post by iamthwee
0

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.

Edited by minitauros

0

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';
}  
0

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).

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.