regular expression AND required validator together?

K

Keith G Hicks

It seems like it would be a reasonably common thing to need to use a regular
expression validator in combination with a required field validator. I'm
doing that on a "new password" page. But I'd rather only use one validador.
Putting 2 validator controls side by side looks funny. For example:

<asp:TextBox ID="txtUserPwd" runat="server" TextMode="Password"
Width="230px"></asp:TextBox><asp:RegularExpressionValidator ID="revUserPwd"
runat="server" ControlToValidate="txtUserPwd" ErrorMessage="Password must be
at least 8 characters in length, contain at least 1 upper case letter, at
least 1 lower case letter and at least 1 digit."
ValidationExpression="^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$">*</asp:
RegularExpressionValidator><asp:RequiredFieldValidatorID="rfvUserPwd"
runat="server" ControlToValidate="txtUserPwd" ErrorMessage="Password is
required.">*</asp:RequiredFieldValidator></td>

When the text box is left blank, the asterisk shows up too far to the right
of the text box. The problem isn't really if there's only one text box to
validate but if you have 3 or 4 stacked on top of each other. If one text
box only has a required field validator, then my asterisks don't line up
vertically. It looks crude that way.

Bottom line is I need to know if it's possible using regular expressions to
also require the value. What can I add to this:

^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$

to make it required. I'm not great with regular expressions.

Thanks,

Keith
 
K

Keith G Hicks

ASP.net 2.0 by the way.

I managed to find a few similar posts and it seems like I'm stuck. Who at MS
had the limited thinking to NOT include a "required" property in the regular
expression validator????? Sure seems pretty logical. Hopefully someone has
an answer. Will wait patiently :)

Keith
 
S

Scott Roberts

Keith G Hicks said:
It seems like it would be a reasonably common thing to need to use a
regular
expression validator in combination with a required field validator. I'm
doing that on a "new password" page. But I'd rather only use one
validador.
Putting 2 validator controls side by side looks funny. For example:

<asp:TextBox ID="txtUserPwd" runat="server" TextMode="Password"
Width="230px"></asp:TextBox><asp:RegularExpressionValidator
ID="revUserPwd"
runat="server" ControlToValidate="txtUserPwd" ErrorMessage="Password must
be
at least 8 characters in length, contain at least 1 upper case letter, at
least 1 lower case letter and at least 1 digit."
ValidationExpression="^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$">*</asp:
RegularExpressionValidator><asp:RequiredFieldValidatorID="rfvUserPwd"
runat="server" ControlToValidate="txtUserPwd" ErrorMessage="Password is
required.">*</asp:RequiredFieldValidator></td>

When the text box is left blank, the asterisk shows up too far to the
right
of the text box. The problem isn't really if there's only one text box to
validate but if you have 3 or 4 stacked on top of each other. If one text
box only has a required field validator, then my asterisks don't line up
vertically. It looks crude that way.

Bottom line is I need to know if it's possible using regular expressions
to
also require the value. What can I add to this:

^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$

to make it required. I'm not great with regular expressions.

Thanks,

Keith


Does setting Display="Dynamic" on your validator(s) help? Or swapping the
placement of the two validators?
 
J

Jeff Dillon

I had the exact same situation. I was surprised it wasn't supported
natively. I wrote a Custom validator (very easy to do...)

Jeff
 
K

Keith G Hicks

Jeff,

Check out Scott Roberts' solution (first person that answered my post). That
handled my problem fine. Here's the help file info on "Dynamic":

Dynamic specifies that you want to dynamically place error messages on the
Web page when validation fails. Space for the validation content is not
allocated on the page; therefore, the page dynamically changes to display
the error message. This allows multiple validators to share the same
physical location on the page. In order to keep the page layout from
changing when an error message is displayed, the HTML element containing the
validator must be sized large enough to accommodate the maximum size of the
validator.



Keith

Jeff Dillon said:
I had the exact same situation. I was surprised it wasn't supported
natively. I wrote a Custom validator (very easy to do...)

Jeff

Keith G Hicks said:
It seems like it would be a reasonably common thing to need to use a
regular
expression validator in combination with a required field validator. I'm
doing that on a "new password" page. But I'd rather only use one
validador.
Putting 2 validator controls side by side looks funny. For example:

<asp:TextBox ID="txtUserPwd" runat="server" TextMode="Password"
Width="230px"></asp:TextBox><asp:RegularExpressionValidator
ID="revUserPwd"
runat="server" ControlToValidate="txtUserPwd" ErrorMessage="Password must
be
at least 8 characters in length, contain at least 1 upper case letter, at
least 1 lower case letter and at least 1 digit."
ValidationExpression="^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$">*</asp:
RegularExpressionValidator><asp:RequiredFieldValidatorID="rfvUserPwd"
runat="server" ControlToValidate="txtUserPwd" ErrorMessage="Password is
required.">*</asp:RequiredFieldValidator></td>

When the text box is left blank, the asterisk shows up too far to the
right
of the text box. The problem isn't really if there's only one text box to
validate but if you have 3 or 4 stacked on top of each other. If one text
box only has a required field validator, then my asterisks don't line up
vertically. It looks crude that way.

Bottom line is I need to know if it's possible using regular expressions
to
also require the value. What can I add to this:

^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$

to make it required. I'm not great with regular expressions.

Thanks,

Keith
 
H

Harlan Messinger

Keith said:
It seems like it would be a reasonably common thing to need to use a regular
expression validator in combination with a required field validator. I'm
doing that on a "new password" page. But I'd rather only use one validador.
Putting 2 validator controls side by side looks funny. For example:

<asp:TextBox ID="txtUserPwd" runat="server" TextMode="Password"
Width="230px"></asp:TextBox><asp:RegularExpressionValidator ID="revUserPwd"
runat="server" ControlToValidate="txtUserPwd" ErrorMessage="Password must be
at least 8 characters in length, contain at least 1 upper case letter, at
least 1 lower case letter and at least 1 digit."
ValidationExpression="^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$">*</asp:
RegularExpressionValidator><asp:RequiredFieldValidatorID="rfvUserPwd"
runat="server" ControlToValidate="txtUserPwd" ErrorMessage="Password is
required.">*</asp:RequiredFieldValidator></td>

When the text box is left blank, the asterisk shows up too far to the right
of the text box. The problem isn't really if there's only one text box to
validate but if you have 3 or 4 stacked on top of each other. If one text
box only has a required field validator, then my asterisks don't line up
vertically. It looks crude that way.

Bottom line is I need to know if it's possible using regular expressions to
also require the value. What can I add to this:

I don't understand. Since your regular expression rejects any string
with n < 8 characters, doesn't it already reject any string with 0 < 8
characters?
 
J

Jeff Dillon

Good to know! Thanks for the kind response...

Jeff

Keith G Hicks said:
Jeff,

Check out Scott Roberts' solution (first person that answered my post).
That
handled my problem fine. Here's the help file info on "Dynamic":

Dynamic specifies that you want to dynamically place error messages on the
Web page when validation fails. Space for the validation content is not
allocated on the page; therefore, the page dynamically changes to display
the error message. This allows multiple validators to share the same
physical location on the page. In order to keep the page layout from
changing when an error message is displayed, the HTML element containing
the
validator must be sized large enough to accommodate the maximum size of
the
validator.



Keith

Jeff Dillon said:
I had the exact same situation. I was surprised it wasn't supported
natively. I wrote a Custom validator (very easy to do...)

Jeff

Keith G Hicks said:
It seems like it would be a reasonably common thing to need to use a
regular
expression validator in combination with a required field validator.
I'm
doing that on a "new password" page. But I'd rather only use one
validador.
Putting 2 validator controls side by side looks funny. For example:

<asp:TextBox ID="txtUserPwd" runat="server" TextMode="Password"
Width="230px"></asp:TextBox><asp:RegularExpressionValidator
ID="revUserPwd"
runat="server" ControlToValidate="txtUserPwd" ErrorMessage="Password must
be
at least 8 characters in length, contain at least 1 upper case letter, at
least 1 lower case letter and at least 1 digit."
ValidationExpression="^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$">*</asp:
RegularExpressionValidator><asp:RequiredFieldValidatorID="rfvUserPwd"
runat="server" ControlToValidate="txtUserPwd" ErrorMessage="Password is
required.">*</asp:RequiredFieldValidator></td>

When the text box is left blank, the asterisk shows up too far to the
right
of the text box. The problem isn't really if there's only one text box to
validate but if you have 3 or 4 stacked on top of each other. If one text
box only has a required field validator, then my asterisks don't line
up
vertically. It looks crude that way.

Bottom line is I need to know if it's possible using regular
expressions
to
also require the value. What can I add to this:

^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$

to make it required. I'm not great with regular expressions.

Thanks,

Keith
 
K

Keith G Hicks

No. They don't work that way. At least not from my experience. I should have
realized that from working with mask editors in Delphi. Just because
something's masked (or in this case validated) to have a certain
configuration of characters, it doesn't mean it can't be null. They're not
really the same thing. I do still feel though that the regular expression
validator should have a "required" property so that this is handled in one
object instead of 2. It's just logical to me since it seems like a common
need.

Harlan Messinger said:
Keith said:
It seems like it would be a reasonably common thing to need to use a regular
expression validator in combination with a required field validator. I'm
doing that on a "new password" page. But I'd rather only use one validador.
Putting 2 validator controls side by side looks funny. For example:

<asp:TextBox ID="txtUserPwd" runat="server" TextMode="Password"
Width="230px"></asp:TextBox><asp:RegularExpressionValidator ID="revUserPwd"
runat="server" ControlToValidate="txtUserPwd" ErrorMessage="Password must be
at least 8 characters in length, contain at least 1 upper case letter, at
least 1 lower case letter and at least 1 digit."
ValidationExpression="^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$">*</asp:
RegularExpressionValidator><asp:RequiredFieldValidatorID="rfvUserPwd"
runat="server" ControlToValidate="txtUserPwd" ErrorMessage="Password is
required.">*</asp:RequiredFieldValidator></td>

When the text box is left blank, the asterisk shows up too far to the right
of the text box. The problem isn't really if there's only one text box to
validate but if you have 3 or 4 stacked on top of each other. If one text
box only has a required field validator, then my asterisks don't line up
vertically. It looks crude that way.

Bottom line is I need to know if it's possible using regular expressions to
also require the value. What can I add to this:

I don't understand. Since your regular expression rejects any string
with n < 8 characters, doesn't it already reject any string with 0 < 8
characters?
 
J

Jesse Houwing

Hello Keith,
No. They don't work that way. At least not from my experience. I
should have realized that from working with mask editors in Delphi.
Just because something's masked (or in this case validated) to have a
certain configuration of characters, it doesn't mean it can't be null.
They're not really the same thing. I do still feel though that the
regular expression validator should have a "required" property so that
this is handled in one object instead of 2. It's just logical to me
since it seems like a common need.

This has not been done, so that the message you will provide to your user
is as accurate as possible at th etime the validation fails.

if it was required, then the message can be: This is a required field.
if the format is incorrect, then the message can be: The field has an incorrect
value. It should look like x-y-z.

They could have put a RequiredText and a RequiredErrorMessage together with
a FormatText and FormatErrorMessage property on there, but that would have
broken with all the other validators.

If you have a customvalidator, you can set the ValidateIfEmpty property to
true, that way it will still fire your own validation scripts, but that should
be used to check combinations of different fields or very specific formatting
issues.

So that is why.

Jesse
regular
validador.
ID="revUserPwd"
must be
at least 8 characters in length, contain at least 1 upper case
letter,
at
least 1 lower case letter and at least 1 digit."
ValidationExpression="^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$">*
</asp:
RegularExpressionValidator><asp:RequiredFieldValidatorID="rfvUserPwd
" runat="server" ControlToValidate="txtUserPwd"
ErrorMessage="Password is
required.">*</asp:RequiredFieldValidator></td>

When the text box is left blank, the asterisk shows up too far to
the
right
of the text box. The problem isn't really if there's only one text
box
to
validate but if you have 3 or 4 stacked on top of each other. If one
text
box only has a required field validator, then my asterisks don't
line up vertically. It looks crude that way.

Bottom line is I need to know if it's possible using regular
expressions
to
also require the value. What can I add to this:
I don't understand. Since your regular expression rejects any string
with n < 8 characters, doesn't it already reject any string with 0 <
8 characters?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top