0

I have a registration page that internal users must use to register to use the site. I'm using javascript to make sure all required field are populated.

I just ran into an issue where a users browser has javascript diabled.

Now I want to be able to check if the browser has javascript disabled I want to do the check on the server and if a required field is empty I will send a message back to the user to populate that field.

Thank you

Jerry Warra
  • 304
  • 1
  • 4
  • 20
  • 1
    You want to execute some JavaScript to see if JavaScript is enabled? Anyway, you should be doing server-side validations anyway since you are handling user input. – takendarkk Jan 23 '17 at 21:00
  • you can use noscript to handle this case as it states [here](http://stackoverflow.com/q/121203/2401386) – Blauharley Jan 23 '17 at 21:01
  • takendrakk, I never said I want javascript to see if javascript is enabled. I asked how can I check if javascript is enabled. What can I use to do that check? Then what can I do to make these check on the server side. Why do I want to go to the server if the fields are empty on the client. – Jerry Warra Jan 23 '17 at 21:05
  • 1
    This use case doesn't really need to check if javascript is enabled - the server must verify all fields anyway every time, it's basic security hygiene. – Peteris Jan 23 '17 at 21:10
  • The short answer: you don't. You have a barebones HTML `
    ` version with a submit that calls the appropriate server script, which you then add to / replace with fancier if JavaScript is available. Or you have a message saying "Requires JavaScript" that gets overwritten (via JavaScript) with the content.
    – Jared Smith Jan 23 '17 at 21:11
  • 1
    Please tell me you realize that even if I have javascript enabled I can change your script and submit whatever I want anyway. That's why you also do server side validations always. – takendarkk Jan 23 '17 at 21:28
  • Takendarkk, How is that possible to change my javascript? I do understand and now it's clear that I need server side validation as well. This way even if javascript is disabled the check will be done on the server and if javascript is enabled it will make sure again if the data conforms to what is expected. – Jerry Warra Jan 26 '17 at 16:16

2 Answers2

0

If you want to show validation messages without javascript - you need to validate forms on server (actually, you need to always valdiate forms server-side) and generate different HTML.

It depends on framework/language you are using to generate HTML.

For example in MVC/C# you will be using something like Html.ValidationMessageFor(m => m.whatever)

You actually should keep them both: javascript (for those who use javascript) to prevent sending wrong data to server.

And then on server you double-check and return modified html

CasHTusK
  • 1
  • 3
0

Typically if a user has javascript disabled on their session/browser I will direct them to either enable it before proceeding. If your form is not using Ajax or rather is not "reliant" on Ajax then it's possible to formulate a response back to the user.

if (is_ajax()) {

//Do your normal processing

} else {

//Validate each input server side, you have to write your own validation here.

$response = array();
if (is_string($_REQUEST['input:name'])) {

} else {
 $response['error'] = true;
 $response['errorMessage'] = 'Your error message.';
 //Handle getting this back to the front end of your registration page.
}

}
Nicholas Koskowski
  • 793
  • 1
  • 4
  • 23