A question about validate

W

wolf

There are three webcontrol on my asp.net form: a TextBox, a submit button
and a RegularExpressionValidator. And I had set ControlToValidate property
of the RegularExpressionValidator to the TextBox. But now, when I input a
string in the textbox and press Enter key, the form was submit without
client javascript validate.

Why? And what should I do to enable client validate before submit the form
whethe press Enter key or click Submit button?

Thanks!
 
J

John Saunders

wolf said:
There are three webcontrol on my asp.net form: a TextBox, a submit button
and a RegularExpressionValidator. And I had set ControlToValidate property
of the RegularExpressionValidator to the TextBox. But now, when I input a
string in the textbox and press Enter key, the form was submit without
client javascript validate.

Why? And what should I do to enable client validate before submit the form
whethe press Enter key or click Submit button?

When you say a Submit button, do you mean <input type="submit"> ? If so, how
would ASP.NET know when that button is clicked so that it can do the
client-side validation?

Try using an <asp:Button> and see if that fixes the problem.

John Saunders
 
W

wolf

When you say a Submit button said:
would ASP.NET know when that button is clicked so that it can do the
client-side validation?

Try using an <asp:Button> and see if that fixes the problem.

John Saunders

No, all of the control are server side web control, and the following is my
code in WebPage1.aspx:


<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="web1.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="ËÎÌå">
<asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 48px; POSITION:
absolute; TOP: 56px" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator id="RegularExpressionValidator1"
style="Z-INDEX: 103; LEFT: 216px; POSITION: absolute; TOP: 56px"
runat="server" ErrorMessage="RegularExpressionValidator"
ControlToValidate="TextBox1"
ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:Reg
ularExpressionValidator>
<asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 144px; POSITION:
absolute; TOP: 96px" runat="server"
Text="Button"></asp:Button></FONT>
</form>
</body>
</HTML>
 
W

wolf

My goal is that the data should be valid if they are submitted to the server
side. But the data will be submitted before validate if I press Enter key
instead of submit button.
 
H

Hans Kesting

wolf said:
There are three webcontrol on my asp.net form: a TextBox, a submit
button and a RegularExpressionValidator. And I had set
ControlToValidate property of the RegularExpressionValidator to the
TextBox. But now, when I input a string in the textbox and press
Enter key, the form was submit without client javascript validate.

Why? And what should I do to enable client validate before submit the
form whethe press Enter key or click Submit button?

Thanks!

I remember something about if a form contains exactly one textbox,
the enter-key will submit the form. This will submit as if a <input type=submit>
was clicked, so any client-side validators will be bypassed.
I do not know if there are ways around it (other that adding a second textbox).

The server-side version of those same validators should still work so
you might want to test for "IsPostback" and "Page.IsValid" to block
unwanted actions.

Hans Kesting
 
G

Guest

Hi,

I think you should set the CausesValidation property of the submit button
to true.

Regards,
Prakash.C
 
G

Guest

Hi,

I think you should set the CausesValidation property of the Submit Button
to true.

Prakash.C
 
G

Guest

Hi,

I think you should set the "CausesValidation" property of the submit
button to true.

Prakash.C
 
G

Guest

Hi,

I think you should set the "CausesValidation" property of the Submit
Button to true.

Prakash.C
 
J

John Saunders

Hans Kesting said:
I remember something about if a form contains exactly one textbox,
the enter-key will submit the form. This will submit as if a <input
type=submit>
was clicked, so any client-side validators will be bypassed.
I do not know if there are ways around it (other that adding a second
textbox).

I vaguely remember this. If my memory is correct, you should try adding
another TextBox control. If that solves the problem, try setting the style
on the dummy textbox to "display: none" so that it does not appear. Setting
Visible to false will cause the textbox to not be rendered to the html, so
that wouldn't work.

John Saunders
 
P

Peter Blum

Sorry for jumping in so late but the problem is quite different from what
everyone is discussing.

Internet Explorer has a strange quirk. When there is only one data entry
control on the form and you hit ENTER, it submits the page without clicking
the submit button. That means, it does not fire any javascript associated
with the button's onclick event. The submit button uses the onclick event to
fire the client-side validation. So that is skipped. In addition, it calls
the __doPostBack() function which prepares some hidden fields for the server
side to know which button was clicked. Since they have not been prepared,
the server side doesn't know to run your button's Click event. Internally,
the button's Click event runs Page.Validate(). Then it calls your method
where you test Page.IsValid is true then save. So none of this is happening.

There are two solutions:
1. Add another data entry control to your page.

2. Add this line to your page_load() method to prepare the hidden fields for
postback so at least server side validation will occur:
Page.RegisterHiddenField("__EVENTTARGET", button.ClientID)
where button is the submit button control.


--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 

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

Top