mike55 Posted December 15, 2005 Posted December 15, 2005 Hi all, I have the following javascript that prevent someone typing more that 145 characters in a text box. However the problem is that it is for a web page, and needs variables such as the form name etc. How can I implement it on a user control. Am using a string variable and enclosing each line of javascript with the following: script &= " [i]Javascript code[/i] " & vbCrLf I then user the following code to register the entire javascript: If Not Page.IsStartupScriptRegistered(scriptName) Then ' Register the script. Page.RegisterStartupScript(scriptName, script) End If End Sub Here is the javascript that I was using: var MaximumCharacters = "145"; var MaximumWords = "0"; var FormName = "Form1"; var TextFieldName = "txtMessage"; var CharactersTypedFieldName = ""; var CharactersLeftFieldName = "CharsLeft"; var WordsTypedFieldName = ""; var WordsLeftFieldName = ""; var WordsMonitor = 0; var MaxWords = parseInt(MaximumWords); var MaxChars = parseInt(MaximumCharacters); var textfield = 'document.' + FormName + '.' + TextFieldName + '.value'; function WordLengthCheck(s,l) { WordsMonitor = 0; var f = false; var ts = new String(); for(var vi = 0; vi < s.length; vi++) { vs = s.substr(vi,1); if((vs >= 'A' && vs <= 'Z') || (vs >= 'a' && vs <= 'z') || (vs >= '0' && vs <= '9')) { if(f == false) { f = true; WordsMonitor++; if((l > 0) && (WordsMonitor > l)) { s = s.substring(0,ts.length); vi = s.length; WordsMonitor--; } } } else { f = false; } ts += vs; } return s; } // function WordLengthCheck() function CharLengthCheck(s,l) { if(s.length > l) { s = s.substring(0,l); } return s; } // function CharLengthCheck() function InputCharacterLengthCheck() { if(MaxChars <= 0) { return; } var currentstring = new String(); eval('currentstring = ' + textfield); var currentlength = currentstring.length; eval('currentstring = CharLengthCheck(' + textfield + ',' + MaxChars + ')'); if(CharactersLeftFieldName.length > 0) { var left = 0; eval('left = ' + MaxChars + ' - ' + textfield + '.length'); if(left < 0) { left = 0; } eval('document.' + FormName + '.' + CharactersLeftFieldName + '.value = ' + left); if(currentstring.length < currentlength) { eval(textfield + ' = currentstring.substring(0)'); } } if(CharactersTypedFieldName.length > 0) { eval('document.' + FormName + '.' + CharactersTypedFieldName + '.value = ' + textfield + '.length'); if(currentstring.length < currentlength) { eval(textfield + ' = currentstring.substring(0)'); } } } // function InputCharacterLengthCheck() function InputWordLengthCheck() { if(MaxWords <= 0) { return; } var currentstring = new String(); eval('currentstring = ' + textfield); var currentlength = currentstring.length; eval('currentstring = WordLengthCheck(' + textfield + ',' + MaxWords + ')'); if (WordsLeftFieldName.length > 0) { var left = MaxWords - WordsMonitor; if(left < 0) { left = 0; } eval('document.' + FormName + '.' + WordsLeftFieldName + '.value = ' + left); if(currentstring.length < currentlength) { eval(textfield + ' = currentstring.substring(0)'); } } if (WordsTypedFieldName.length > 0) { eval('document.' + FormName + '.' + WordsTypedFieldName + '.value = ' + WordsMonitor); if(currentstring.length < currentlength) { eval(textfield + ' = currentstring.substring(0)'); } } } // function InputWordLengthCheck() function InputLengthCheck() { InputCharacterLengthCheck(); InputWordLengthCheck(); } // function InputLengthCheck() //--> Any suggestions would be appreciated. Mike55. Quote A Client refers to the person who incurs the development cost. A Customer refers to the person that pays to use the product. ------ My software never has bugs. It just develops random features. (Mosabama vbforums.com)
Nate Bross Posted December 15, 2005 Posted December 15, 2005 Why not use the property of the input box? <input type="text" value="" maxlength="145"> Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Nate Bross Posted December 16, 2005 Posted December 16, 2005 Let me know if that works, I may have misunderstood your problem, and over simplifyed the answer. Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Nate Bross Posted December 16, 2005 Posted December 16, 2005 By the way, congrats Diesel on 500 posts even, at least at 10:23 (-7:00)December 15th ;) Didn't you also used to live in Lombard IL? Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Diesel Posted December 16, 2005 Posted December 16, 2005 Yessir. Been in San Diego about a month now. Fun place. Quote
mike55 Posted December 19, 2005 Author Posted December 19, 2005 The problem with setting the value in the code behind is that I am using a multi-line textbox, and I also I need to display the number of characters the user has left. Displaying the number of characters is optional. Mike55. Quote A Client refers to the person who incurs the development cost. A Customer refers to the person that pays to use the product. ------ My software never has bugs. It just develops random features. (Mosabama vbforums.com)
Nate Bross Posted December 20, 2005 Posted December 20, 2005 What is the exact error you are getting? Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
HJB417 Posted December 21, 2005 Posted December 21, 2005 I would think that setting the value in code behind would cause the the creation of the maxlength attribute by the asp.net runtime. DOM had access to the attributes of elements. Quote
Diesel Posted December 21, 2005 Posted December 21, 2005 HJ, what would that matter if the browser has no idea what to do with the attribute...anyway, do a search for textarea maxlength, there's a lot of javascript functions that will accomplish what Mike wants to do. I put a link up in his re-post of this topic. Quote
HJB417 Posted December 21, 2005 Posted December 21, 2005 The browser knows what to do with the maxlength attribute. It's a simple (not the best) way of preventing buffer overflow. http://www.smartwebby.com/DHTML/textbox_characters_counter.asp Also, since the asp.net runtime will emit an autogenerated name for the control. It can be accessed before Render using Control.ClientID Quote
Diesel Posted December 21, 2005 Posted December 21, 2005 Dude, the link you posted has nothing to do with the browser. It's a javascript function that uses the maxlength attribute. It's a good idea though...instead of passing the value everytime you call the function. Quote
HJB417 Posted December 22, 2005 Posted December 22, 2005 ya, exactly. Maybe saying 'browser' wasn't the best way to say what I meant. I hinted javascript when I said 'DOM had access to the attributes of elements.' I know his code will require a bit more trickery since the id of the control is being created by the runtime. I think what I've done in the past was create a global javascript variable that was either a struct/class or a hashtable that was able to get the clientId/true id of a control. Quote
Diesel Posted December 22, 2005 Posted December 22, 2005 No, that code should work as is for his control. When you call the javascript function from the control, the control is passed in as an object through the event var, so no id is needed. Quote
HJB417 Posted December 22, 2005 Posted December 22, 2005 No, that code should work as is for his control. When you call the javascript function from the control, the control is passed in as an object through the event var, so no id is needed. sweet, i didn't know that. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.