最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is restriction using input maxlength sufficient enough? - Stack Overflow

programmeradmin6浏览0评论

Is it sufficient to restrict user input value by setting maxlength only? Lets say I have this code:

    <input type="text" id="foo" maxlength="12">

Is there any possibility that user still can (in any valid or invalid way) insert value more than 12?

When we have set the maxlength, is it usefull or useless to validate it once again using javascript or maybe at the backend (servlet, etc)?

Is it sufficient to restrict user input value by setting maxlength only? Lets say I have this code:

    <input type="text" id="foo" maxlength="12">

Is there any possibility that user still can (in any valid or invalid way) insert value more than 12?

When we have set the maxlength, is it usefull or useless to validate it once again using javascript or maybe at the backend (servlet, etc)?

Share Improve this question asked Jan 7, 2014 at 7:11 DnRDnR 3,5173 gold badges25 silver badges33 bronze badges 3
  • 2 "maxlength" only checks the frontend part, but you need to check what arrives at the server. – Rob Commented Jan 7, 2014 at 7:15
  • 3 Just remember this: Anything on the client side can easily be defeated /spoofed. Always perform thorough validation on the server side. Never trust anything from the user. – Jonathon Reinhart Commented Jan 7, 2014 at 7:20
  • On client side by using browser development tools like 'firebug' anyone can easily remove 'maxlength' attribute. So there is need to add server side validations. – mujaffars Commented Jan 7, 2014 at 7:26
Add a ment  | 

2 Answers 2

Reset to default 10

Is it sufficient to restrict user input value by setting maxlength only?

No

Is there any possibility that user still can (in any valid or invalid way) insert value more than 12?

Yes

When we have set the maxlength, is it usefull or useless to validate it once again using javascript or maybe at the backend (servlet, etc)?

You should validate, and preferrably on the backend.


That's because you don't necessarily need a browser to pass data to the server. There are other client software, like REST testers, curl, wget, tamper data and similar software that can fire requests directly to the server, all of which bypass your maxlength attribute and JS validations.

So if you want fast validation so that the user gets a snappy, interactive response, your maxlength and JS validations does that job. But you should do a second validation when the data is passed to the server, this time for security.

It is all upon you. Choose your datatype allowing only 12 values in database.

You job on client side is done after validation but database won't be saving values more than 12.

发布评论

评论列表(0)

  1. 暂无评论