Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 13th, 2008, 06:13 PM
Authorized User
 
Join Date: Apr 2008
Location: , , .
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to AspNetGuy
Default Chapter 9, page 304 Again

Ok, so NOW I understand the line of code in the script that begins as follows:
var txtPhoneHome = document.getElementById( ... );

Actually, I finally read ALL the text IMAR wrote about the JavaScript and THEN I understood it. RTFM...Read The Fine Manual...

What I want to do, in as generic a way as possible, is to trim the text of all TextBoxes on the page. There could be one or many TextBoxes. I could add a CustomValidator for each TextBox and have each CustomeValidator call a specific unique function, though that seems kind of silly.

If I call one function once for all TextBoxes then the JavaScript would need to know the names of all the TextBoxes. I am less hesitant to implement this particular method in C# since the names of the TextBoxes are not mangled there.

Any guidance on how to proceed would be greatly appreciated.

And Imar, if you answer this, thanks again for all the help!!!!

Reply With Quote
  #2 (permalink)  
Old June 15th, 2008, 01:50 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

Glad it all makes sense now.....

There's no need to know all the names. You can simply loop through all the controls in a page using client side JavaScript, then see if the control is a text box (type="text") and act accordingly. The following code demonstrates how this works, and alerts the value of any text box that is found on the page:
Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">
        function Do() {
            var allControls = document.getElementsByTagName('input');
            for (var i = 0; i < allControls.length; i++) 
            {
                if (allControls[i].type == 'text') 
                {
                    var myTextbox = allControls[i];
                    alert(myTextbox.value);
                }
            }
        }
    </script>

</head>
<body>
    <form method="post">
        <input type="text" id="txt1" />
        <input type="text" id="txt2" />
        <input type="submit" onclick="Do();" />
    </form>
</body>
</html>
Hope this helps,

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
Reply With Quote
  #3 (permalink)  
Old June 15th, 2008, 02:32 PM
Authorized User
 
Join Date: Apr 2008
Location: , , .
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to AspNetGuy
Default

Thanks Imar,

Over the weekend I discovered a solution similar to the one you suggested and it is a great way to trim blanks or do whatever to ALL TextBoxes. I have implemented that solution and will keep it in my toolbag. This solves one of my problems but I still have one problem hanging around.

With the solution above one would invoke the Javascript at Submit button click time or in my case with a wizard at Next button click time. For general purposes this works well, but....

Suppose we have a RegularExpression validator for an email address and a CompareValidator for a second email address. This comes up a lot when you are making sure the user enters an accurate email address. Suppose the user types in the correct email address on the 1st field and the exact same email address on the second field, but adds a trailing blank.

The first field will validate correctly with the RegularExpressionValidator, but the compare will fail since the second email address has a trailing blank. The message would say something like the two email addresses are not equal. Even if we add a RegularExpressionValidator to the second email address the best it would be able to identify is "invalid format" since any number of problems could go wrong.

The user on the other hand is looking at the two addresses and can not SEE the problem.

Can I use a RegularExpressionValidator to change the content of the TextBox? I am using a regular expression in JavaScript to trim the blanks so I thought, if I could get a RegularExpressionValidator to trim the blanks then problem solved.

The idea is to trim the blanks either before or after the RequiredValidator runs using a RegularExpressionValidator. Then run a second RegularExpressionValidator to enforce rules about the content of the TextBox.

My goal is to remove the blanks before a validation error occurs, or to identity the blanks as present and invalid. This would still require a second RegularExpressionValidator to identify the error as leading or trailing blanks.

I realize I could use a CustomValidator for this purpose but then we are back to knowing the names of all the fields since the CustomValidator would need to obtain the control.

BOTTOM LINE:

Can I use a RegularExpressionValidator to change the content of the TextBox?

Thanks for any help you might give.



Reply With Quote
  #4 (permalink)  
Old June 15th, 2008, 02:57 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Maybe you can get away using your own function instead of using another validator? Simply write a handler for the onblur event that fires when the user moves away from a control:

[Server Side C#]
TextBox1.Attributes.Add("onblur", "Trim(this);");
TextBox2.Attributes.Add("onblur", "Trim(this);");

[Client side JavaScript]
function Trim(control)
{
  alert(control.value);
  // trim here
}

Personally, I wouldn't bother with this too much. Users typically have an idea of what they are typing. If they are lost, they'll enter the entire e-mail address again. While they could make the same mistake again, I doubt this is a common problem....

Hope this helps,


Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 9 Page 304 AspNetGuy BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 12 December 22nd, 2009 11:17 AM
Chapter 3 Page 95 jumpot89 BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 6 April 22nd, 2009 03:11 AM
Chapter 9 JavaScript Warning Message Page 304 workib BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 5 May 28th, 2008 04:42 PM
Chapter 7 page 212 tgregory BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 4 May 3rd, 2007 01:44 PM
Chapter 7 Try It Out page 218 drumdiva BOOK: Beginning Visual Basic 2005 ISBN: 978-0-7645-7401-6 3 March 19th, 2007 08:35 PM



All times are GMT -4. The time now is 05:19 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.