Jump to content

Expression verification help


Recommended Posts

Hi all. After being away from coding and trying this for a while I have been asked to assist someone with something and I am not winning.....or understanding how to check the form validation coming though. The 'friend/client' needs to check that the form field filled in begins with 'DT' (either upper case or lower case) can have a space(or not) then a series of 7 numbers. For example 'DT 1234567' or 'dt1234567'

I am really not understanding how to do this. I have started with some sample code

/**
 * Validate UM DT Number field for registration dt_number2
 * @param string $key
 * @param attay  $array
 * @param array  $args
 */
function um_custom_validate_dt_number2( $key, $array, $args ) {
	if ( isset( $args[$key] ) && !preg_match('/^[6-9]\d{9}$/', $args[$key]) ) {
		UM()->form()->add_error( $key, __( 'Please enter a valid DT Number.', 'ultimate-member' ) );
	}
}
add_action( 'um_custom_field_validation_dt_number2', 'um_custom_validate_dt_number2', 30, 3 );

but for the life of me, everything I have tried on sites like http://www.santic.org/preg_match/ just dont work, I get errors all the time. The best I can do is check that there is a DT in the string!

/(TD)\w*/i 

checked against my string TD 1234567, 1236547 TD, 123DT4567 gives me 2 results, but as soon as I try check for numbers etc

/(TD)+[0-9]\d{7}\w*/i

 I am failing. Please someone guide me!

Link to post
Share on other sites
/^DT ?\d{7}$/i

Details:

  1. ^ - Matches the beginning of the input string.
  2. DT - Matches the literal string DT
  3. <space>? - Matches a zero or one space character.
  4. \d{7} - Matches seven digit characters.
  5. $ - Matches the end of the input string.
  6. i - Makes the match case in-sensitive so it'll match lowercase dt as well.

I would suggest using a better online tool when building and testing your expressions, such as the one linked above.  It provides a breakdown of what the expression means as well as references to consult.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.