Click here to Skip to main content
15,905,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hii ,
i have many control in my form , As per I know i set required fieldvalidator control for each control and set text property to * and error message property with proper error which will get displayed into summary control , but due to some unknown reason star mark is not gets displayed before entering , it only executes after clicking on button , please suggest me something else
Posted

Text property hold the message to display when the validation fails.
So when you click the button and validation fails you see what you have
set in Text property.

To have '*' initially, have it in a label that might be before/after your
input element.
 
Share this answer
 
v2
Comments
Torakami 10-Jul-13 9:28am    
Ok I Agree with this solution , But I have lots of controls in my form , so all time visible disable this label is not possible , its would be very tough
Samresh.ss 10-Jul-13 9:38am    
You might want to use CustomValidator then with a single Client validation function for all.
The function would check if the input element holds something, if not pick its previous element (label) and add a '*' if not there, else remove the '*'
Torakami 11-Jul-13 1:11am    
Can You provide me the code to understand your logic ,,
For a sample markup like this

XML
<asp:Label ID="lblName" runat="server" Text="Enter Name:" ValidationGroup="test"></asp:Label>
        <asp:TextBox ID="txtName" runat="server" ValidationGroup="test"></asp:TextBox>
        <asp:Button ID="btnSubmit" runat="server" ValidationGroup="test" />
        <asp:CustomValidator ControlToValidate="txtName" ID="cvName" ErrorMessage="error" Text="error"
         runat="server" ClientValidationFunction="checkIfValExists" ValidateEmptyText="true" ValidationGroup="test"></asp:CustomValidator>



You can use a js function like this (jquery is used in here)

C#
function checkIfValExists(source, args) {
        var txt =$(source).prev().prev().prev().text();
        if($(source).prev().prev().val().length == 0)
        {
            if (txt.length > 0 && txt.charAt(txt.length - 1) != '*') {
                $(source).prev().prev().prev().text(txt + '*');
            }
            args.IsValid = false;
        }
        else
        {
            if(txt.length > 0 && txt.charAt(txt.length - 1) == '*' ){
                $(source).prev().prev().prev().text(txt.substring(0, txt.length - 1));
            }
        }
    }
 
Share this answer
 
Comments
Torakami 12-Jul-13 1:31am    
can you please explain me this line ..

var txt =$(source).prev().prev().prev().text();
Samresh.ss 12-Jul-13 2:05am    
$(source) wraps the element that raised your event(your customvalidator) in this case into a jquery object. This allows you to use jquery methods to your objects. prev() gives your previous element in DOM. so calling prev() on prev() gives you previous to previous element.

So: $(source).prev().prev().prev().text(); gives you text of your label.

You can get the element by other methods also. I used this approach so that the same method can be used for all your input elements in the form.
Torakami 12-Jul-13 2:54am    
ok ... thank you so much ..
Torakami 12-Jul-13 7:03am    
I am simply using * mark and was trying to remove text property from required field validator , which was set to * previously , but now error message gets displayed when i click on button , I want to show error message in summary only not in near control as i am manually givinf star mark their , how could i do that
Samresh.ss 12-Jul-13 8:34am    
Remove the Text property and add the ErrorMessage property

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900