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:
I then user the following code to register the entire javascript:
Here is the javascript that I was using:
Any suggestions would be appreciated.
Mike55.
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:
Code:
script &= " [I]Javascript code[/I] " & vbCrLf
I then user the following code to register the entire javascript:
Code:
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:
Code:
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.