I'm trying to set a minimum password lenght for a login page; however, it seems that the only attribute the password (input) tag has is maxlength. I know you can use the new pattern or min attribute, but I need something that works in IE 9. Is there a simple/replacement way to solve this or will I have to use strlen?
Asked
Active
Viewed 1.3k times
3
-
2@JonathandeM. What if JS is disabled? – Danny Beckett Jul 01 '13 at 10:06
-
2Use js, but validate server-side as well – Mark Baker Jul 01 '13 at 10:07
-
I'm trying to stay away from javascript when there is much easier ways to solve this issue. – Llama Jul 01 '13 at 10:15
-
Javascript is unsafe for this kind of thing. Only use for client-side operations. – James P. Jul 01 '13 at 10:21
5 Answers
5
Some Ok answers so far.
However, this is much more a concern server-side than client. I would not be difficult to "hack" the markup or Javascript to allow a short password.
You should always check server-side as well to make sure nothing weird is going on.
To sum it all up: Use Javascript to make sure the password is longer than the minimum length and recheck it server-side to make sure the jsfunction was not bypassed in any way.

OptimusCrime
- 14,662
- 13
- 58
- 96
-
_You should always check server-side as well to make sure nothing weird is going on._ +1 – James P. Jul 01 '13 at 10:22
2
One liner:
<input type="text" onKeyPress="return ( this.value.length < 10 );"/>
This can be used to implement maxlength
in Internet Explorer but you should validate the input server side, too.

Amal Murali
- 75,622
- 18
- 128
- 150
2
You must use server side solution for this problem as you never know whether user has js enabled or not..

dhaval
- 176
- 1
- 1
- 7
2
Answer from another question, try this:
<input pattern=".{3,}" required title="3 characters minimum">
<input pattern=".{5,10}" required title="5 to 10 characters">
1
function CheckLength(name) {
var password = document.getElementById(name).value;
if (password.length < 4)
alert('should have miniumum 4 chars');
}
</script>
</head>
<body>
<form id="form1" runat="server">
<input type="password" id="txtpassword" name="txtpassword" />
<input type="submit" name="txtSubmit" onclick="CheckLength('txtpassword') " />

akash
- 2,117
- 4
- 21
- 32
-
Thank you for the answer. I guess the only way in IE 9 and below is to check the array length rather than checking it in the input tag. – Llama Jul 01 '13 at 10:18