PDA

View Full Version : Alert maximum characters in textfield



Yammaski
02-17-2011, 02:29 PM
The alert should appear when the text is longer then the maximum characters for the choosen width.
E.g.: choosen width is 15 cm, the max is 15 characters.

Prototype (http://www.frogstyling.be/X_Tests/maxCharsCheck.asp)

Nearly ...
But in stead of ???, I need the value of "var maxChars".



<script>
<!--
function checkchars(cur){
var e = document.getElementById("FrogStyling_1_Size_Add");
var maxChars = e.options[e.selectedIndex].value;
var maxlength=maxChars

if (cur.AddText.value.length>maxlength){
alert("Please restrain your input to ??? or less characters !")
return false
}
}
//-->
</script>




Yam.

Nile
02-18-2011, 04:19 AM
Why don't you just use the maxlength attribute for inputs?

djr33
02-18-2011, 04:49 AM
Agreed, but here's how to change your code:
alert("Please restrain your input to "+maxChars+" or less characters !");

Also, while the browsers may ignore this and continue working, you have a syntax error when an operation does not end in a semi-colon. For example:
var maxlength=maxChars
or
return false
and the alert, but I added it in my code above.

Add a semi-colon ; to both and it will be valid.

Yammaski
02-18-2011, 08:22 AM
Thanks, it's working now.
But I've 2 functions that I want to work with onsubmit event handler.
I'll make another thread for this : "2 functions onsubmit event handler".


Yam.

Yammaski
02-18-2011, 08:40 AM
Why don't you just use the maxlength attribute for inputs?

Can you give a little more explanation, because I don't get it. :(

Nile
02-18-2011, 01:36 PM
http://www.w3schools.com/tags/att_input_maxlength.asp

Yammaski
02-18-2011, 02:24 PM
I think that isn't the solution I need here.
It's the same textfield for different widths.

Did you try my prototype (http://www.frogstyling.be/X_Tests/maxCharsCheck.asp) ?

Nile
02-20-2011, 04:28 AM
THe reason this isn't working is because there's no element on the page with the ID "FrogStyling_1_Size_Add"

Change:


<select name="FrogStyling_1_Size_Add" onchange="this.form.submit()">

To:

<select id="FrogStyling_1_Size_Add" name="FrogStyling_1_Size_Add" onchange="this.form.submit()">