Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop people getting around my JavaScript Validation?

I have a registration form on my website and I have 3 input boxes for the Date of Birth where are coded like this...

<input name="dd" id="dd" type="text" value="DD" size="2" />
<input name="mm" id="mm" type="text" value="MM" size="2" />
<input name="yyyy" id="yyyy" type="text" value="YYYY" size="4" />

I then have a JavaScript validation page which is run on Submit. This page checks that the fields are not empty and that they are between a certain number e.g. between 1 and 31 for Day
Here is the JavaScript for the 'dd' field...

var nulld=document.forms["create"]["dd"].value;
if (nulld==null || nulld=="")
  {
  alert("Please enter a day between 1 and 31");
  return false;
  }
var dd=document.forms["create"]["dd"].value;
if (dd >= 1 && dd <= 31)
  {}else{
  alert("Please enter a day between 1 and 31");
  return false;
  }

So I have this code, but iv noticed that someone is disabling JavaScript on this computer and is creating loads of accounts with incorrect data. How do I stop this from happening? or is the only way to do it to add code that checks if JavaScript is disabled and divert them to an error page like this...

<noscript><meta http-equiv="refresh" content="1;url=nojavascript.html"></noscript>

Is there another way to do it where users can still create an account without JavaScript but still have the Validation?

like image 355
Ben Avatar asked Dec 01 '25 05:12

Ben


1 Answers

JavaScript validation is good for one thing only: quickly letting users know that the data they are going to submit to the server will be rejected.

You cannot control what comes into your system. You must check that data is suitable on the server before you start processing it.

like image 123
Quentin Avatar answered Dec 02 '25 18:12

Quentin