using regular expressions to validate input?

2 replies [Last post]
Wallace9879
Offline
Last seen: 11 years 1 week ago
Joined: 2011-01-25

Anyone know how to easily add code or use use regular expressions to evaluate input? I think the reg expression is ^\d{5}$|^\d{5}-\d{4}$. Unfortunately, I just don't know how to put that in something drupal will understand.

I have created several basic input values and some need input validation, does anyone know some code for the following:

-- ZIP (US: 5 or 5+4)
-- phone (US: 10 digits include (XXX) XXX-XXXX

to validate a field that is type int, I am using the following drupal code:
function _element_validate_integer_positive($element, &$form_state) {
$value = $element['#value'];
if ($value !== '' && (!is_numeric($value) || intval($value) != $value || $value <= 0)) {
form_error($element, t('%name must be a positive integer.', array('%name' => $element['#title'])));
}
}

For emails I think I can use the following (untested):
function valid_email_address($mail) {
return (bool) filter_var($mail, FILTER_VALIDATE_EMAIL);
}

But can anyone help with code for ZIP or PHONE?

Thanks,

J.

Wallace9879
Offline
Last seen: 11 years 1 week ago
Joined: 2011-01-25
Never mind. I figured out the code. HEre is what I used

in case anyone wants some code:

Zip
This line goes within the element within hook_field_widget_form()

'#element_validate' => array('module_element_validate'),

/**
* Validation module_element_zip is a valid zip element.
*/
function module_element_zip_validate($element, &$form_state) {
if ($element['#value'] !== '' && !preg_match("/^\d{5}$|^\d{5}-\d{4}$/", $element['#value'])) {
form_error($element, t('%name is not a valid ZIP code. Please enter a valid ZIP code.', array('%name' => $element['#title'])));
}
}

I will use something similiar to validate phone numbers.

Reference sites:
http://www.regexlib.com --used to find regular expressions
http://www.regular-expressions.info/javascriptexample.html -- Test regular expressions validation
http://drupal.org/node/862286 -- Discussion which lead me to the code.

Hope this helps someone else.

J.

Chris Shattuck
Offline
Last seen: 10 weeks 3 days ago
Joined: 2009-08-16
Excellent! Thank you for

Excellent! Thank you for posting your solution, Wallace9879!

Cheers,
Chris

Add to playlist

Add to , or

Add to new playlist:

Add to playlist
This is a member-only feature
But, it's easy to become a member

Add to cart:


Loading...
Contact us

Below are the supported keyboard shortcuts for our video player.

space
Start / stop video
shift
Go to next video
shift
Go to previous video
Increase or decrease player speed by 20%
Skip ahead or go back by 15 seconds
1...9
Jump to a percentage (2 is 20%, for example)
f
Toggle full screen
Videos Pricing More Search
BuildAModule Logo Icon BuildAModule Logo Icon
Become a member!
$29
1 month
$156
6 months
10% off
$276
12 months
20% off
All memberships come with the following:
24 / 7 access to the entire 1800+ video library
An innovative video player with a rolling transcript
Extensive Drupal and cross-platform training
A thorough and thoughtful training style
Progress tracking
Take notes on specific video sections
Intuitive multi-user management
How many users?
user
Go solo with a single user, or give multiple team members access through one account and get discounts by bundling multiple users into a single membership.