struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Laurie Harper <lau...@holoweb.net>
Subject Re: How to validate MobileNumber using regex Expression
Date Wed, 12 Dec 2007 20:38:50 GMT
ginu@software.ramapuram.in wrote:
> iam validating mobile number using the regex Expression
>  But even though iam giving a valid number,
> its showing invalid number message
> 
> <field-validator type="fieldexpression">
>                <param name="expression">
>                  (cityLedger.contactDetails.mobileNumber.matches('^(\?([0-9]{3})?([0-9]{4}|[0-9]{4})$'))
>                </param>
>                <message key="errors.phone"/>
>            </field-validator>
>       </field>
> Ginu

You didn't specify what a mobile phone number *should* look like in 
India, but some things that look odd right off:

1) Single quotes around the regex; I'm not sure if that's valid syntax 
for the fieldexpression validator. Try using double quotes instead, as 
in standard Java syntax.

2) Your regex starts '^(\?(...' which, according to Java String literal 
syntax rules is equivalent to '^(?(...' which is not a legal construct. 
If you really want to require a literal '?' as the first character in a 
phone number (which seems odd), you would need to write '^(\\?(...'

3) The remainder of the regex says the input may optionally contain 
three digits, followed by either four digits or... four digits. The two 
alternatives in that final group are identical -- '(X|X)'

Due to (1) I don't think that regex can even be compiled, so it would 
always fail regardless of the input, unless the JVM is being lax and 
treating the '?' as a litteral character. If that's the case then, due 
to (2), this regex will only match input of the form '?####' or 
'?#######', i.e. a question mark followed by either three or seven 
digits, without any spaces.

Depending on what exactly you consider valid inut, this might be more 
appropriate:

     ...matches("^(\d{3} )?\d{4} \d{4}$")

to match either '### #### ####' or '#### ####'.

L.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message