Link to home
Start Free TrialLog in
Avatar of zachvaldez
zachvaldezFlag for United States of America

asked on

validating textboxes

I have a label after a textbox markes '*" as a warning that it is a 'must field'.
When it is populated it removes the '*" and when there is no entry put back the " label.

Can this be done other than the validating controls?
Avatar of Rajkumar Gs
Rajkumar Gs
Flag of India image

Using javascript code, you can achieve this.

Raj
This is a sample code -
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    
    <script language="javascript" type="text/javascript">
        function validate() {
            var textboxValue;
            textboxValue = document.getElementById("TextBox1").value;

            if (textboxValue == "")
                document.getElementById("Label1").innerHTML = "*";
            else
                document.getElementById("Label1").innerHTML = "";
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" OnKeyUp="validate();"></asp:TextBox>
        <asp:Label ID="Label1" runat="server"
            Text="*"></asp:Label>
    </div>
    </form>
</body>
</html>

Open in new window

You can also use something like jformer:

http://www.jformer.com/demos/contact/

to create some nice forms
Avatar of zachvaldez

ASKER

the javascript.. Can that be a universal function not only limited to textbox1? thanks
Yes so long you have it pointing to the correct value for instance if you had a dropdownlist you can use:

$("#dropdownidhere option:selected").text();

instead of

document.getElementById("TextBox1").value;

I am using jquery in this example so you would need a reference to jquery like so:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
nice demo. can that be applied? is it javascript or .net source?
if you look at jformer it has built in validation and is very easy to pass validation on controls like so:

http://www.jformer.com/demos/team/

and you can set min/max etc
it is jquery (javascript) and yes it can be applied you can download it here:

http://www.jformer.com/download/
I already have the <script> tag. Im not familiar with jquery but willing to learn though. should I chaneg somehting on the page script tags to reference it?
which one should I download, the produciton or developer.
What about the jquery link provided there? so its jformer..
you can have multiple script tags, if you want to go that route I would download it and you can see the deomos in your download on how it works.

if you don't want to use the aspx validation controls jquery is a nice solution.  Jquery is a cross-browser javascript library
for jquery don't worry about that you can use

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>

that will take care of jquery for you (hosted by google)

the production download is minified so its optimized for production I would go with that one if you plan to use it on a live website
step by step here..
So I added the library in the page(link you sent)
so what next to implement.

BTw this form will be in a website soon but not now...
so how will I apply this with regards to the textbox validation(my question?)
ASKER CERTIFIED SOLUTION
Avatar of disrupt
disrupt
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Btw how would I force he page or move  ower so  user can see the grid they entered instead of defaulting to top page?