473,320 Members | 1,988 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Using an alertbox when the length is wrong

i put this code but there is a problem. this is not working properly.
this is on textchanged evet .

Expand|Select|Wrap|Line Numbers
  1. protected void t1_TextChanged(object sender, EventArgs e)
  2.     {
  3.         int max = 5;
  4.         int length = t1.Text.Length;
  5.         if (length !=max)
  6.         {
  7.             Response.Write("<script type = text/javascript language = javascript >");
  8.             t1.Attributes.Add("onblur", "alert('" + length + "');");
  9.             //Page.Validate();
  10.          // Response.Write("alert('" + length + "');");  
  11.             //t1.Focus();
  12.  
  13.             Response.Write("</script>");
  14.         }
Nov 10 '09 #1
5 1861
Frinavale
9,735 Expert Mod 8TB
I'm not sure what you're trying to do.
Right now you are handling the .NET "TextChanged" event.
When the text in the TextBox (t1) is changed a postback to the server occurs.
Then your .NET code is executed.

You have a bunch of JavaScript stuff going on here that I can't understand.

JavaScript is run in the web browser (client side).
It is probably best that you use JavaScript to handle the TextChanged event in the browser instead of posting back to the server....so you have the right idea, I just don't know what you're doing.

Another thing that is not a good idea is use the Response.Write() method in your C# code. The reason is because when you use this method in your C# code it is very likely that it will be placed somewhere invalid (like outside of the <html> tag).
For example, if I had the following C# code:
Expand|Select|Wrap|Line Numbers
  1. Response.Write("<script type = 'text/javascript'>");
  2. Response.Write("  alert('Hello World!');");
  3. Response.Write("</script>");
The resulting HTML would be something like:
Expand|Select|Wrap|Line Numbers
  1. <script type='text/javascript'>
  2.   alert('Hello World!');
  3. </script>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. ......
  7.  
Notice that the script is before the <html> tag?
This is makes the web page invalid and some browsers wont even run the code, other browsers will try to run the code but it may not run as expected.


I recommend putting the JavaScript into the ASPX page itself, or placing it into an external JavaScript file.

If you place the JavaScript in the ASPX page itself then you can use ASP to tell the Response.Write method where the content should be placed.

For example, the following JavaScript code is located with my Default.aspx page:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function onTextBoxBlurEventHandler(){
  3.   var textBoxElement = document.getElementById('<% Response.Write(t1.ClientID); %>');
  4.   if(textBoxElement && textBoxElement.value){
  5.     alert("The length is: "+textBoxElement.value.length());
  6.   }
  7. }</script>
Notice how I used the Response.Write method?
I told it to write the ClientID of the t1 TextBox into the getElementById JavaScript method... Now the JavaScript is placed in the right place on the page and everything is valid.


This script doesn't even really need to use the getElementById JavaScript method...and it doesn't require the use of the Response.Write method either.
I would modify the JavaScript function so that it took 1 parameter: the TextBox element.
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function onTextBoxBlurEventHandler(textBoxElement){
  3.   if(textBoxElement && textBoxElement.value){
  4.     alert("The length is: "+textBoxElement.value.length());
  5.   }
  6. }</script>

I would remove the "TextChanged" event from the t1 TextBox and in the page load event I would put:

Expand|Select|Wrap|Line Numbers
  1. t1.Attributes.Add("onblur","onTextBoxBlurEventHandler(this);');
Happy coding,

-Frinny
Nov 10 '09 #2
alert("The length is: "+textBoxElement.value.length());
use it within scripit tag
Nov 12 '09 #3
PRR
750 Expert 512MB
I guess you should look into RequiredFieldValidator, CustomValidator.
Nov 13 '09 #4
Frinavale
9,735 Expert Mod 8TB
Huh, you're right PRR!

I don't think I read the title....I think I just read the description of the problem.

Really the OP should be setting the TextBox.MaxLength Property too.

The thing about client-side validation is that it is quite easy to get get around it. People can even get around the MaxLength if easily they wanted to.

Please remember always do validation server side before using anything provided by the user.

-Frinny
Nov 13 '09 #5
PRR
750 Expert 512MB
@Frinavale
:) your answers are just amazing and very thorough. You really answer them very well. Keep it up.
Yeah i guess textbox.maxlength will do too.
Nov 17 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: beeo | last post by:
Hi What's wrong here? Although "box" is empty the alertbox wont popup! <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html>
3
by: Steve Mauldin | last post by:
I came across an example in the MSDN documentation using RC2 encryption(the link to the article is at the end of this message). When I tried it I had a problem with getting back the same length...
1
by: trapeze.jsg | last post by:
Hi. I am trying to get through to Microsoft MapPoint Services using ZSI for soap handling. I can generate the service classes and also the soap-requests generated by the service classes seem to...
2
by: Chucker | last post by:
Hi Community, I think I can store Binary Data in SQL Server but when I try to retrieve it, I always only get one byte. I think I stored my Binary Data in SQL Server in a Colum of Type Image....
6
by: ransoma22 | last post by:
I developing an application that receive SMS from a connected GSM handphone, e.g Siemens M55, Nokia 6230,etc through the data cable. The application(VB.NET) will receive the SMS automatically,...
9
by: weirdwoolly | last post by:
Hopefully someone will be able to help. I have written a stored procedure in C++ called from a Java test harness to validate the graphic data types in C++ and their use. I have declared the...
8
by: mark4asp | last post by:
How can I get spans which look like buttons. These buttons have a fixed height (24px), variable width, a thin (1px) solid border and a background image. The back-ground image is a slice 24px...
8
by: rob21century | last post by:
Hi every one, newbie here, so i big hello from me now down to business I am a web developer but i don't use java script very often, but i have created a form validation from a group of different...
16
Gwyn Thomas
by: Gwyn Thomas | last post by:
Hi All, Firstly, I apologise if this question has appeared before but I can't find it anywhere. I have created a gridview with two command buttons. When I click the buttons I want either an...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.