I would like to validate and sanitize an input e.g "search" coming from a GET form request but i'm missing something about Javascript pattern matching.
This is the function where i'm currently working on:
function jsValidationAndSanitization() { /** Validate and sanitize every input that comes from an HTML form. @return boolean **/ var submittedInput = document.forms["form"]["search_input"].value; if (submittedInput == "") { alert("error: empty input"); return false; } if (submittedInput != "") { // admitted chars ( white list ) var wl_pattern = /[A-z][0-9]/; // loop for every chars in the submitted string for (char in submittedInput) { // if a bad char is present on the string return false var result = char.match(wl_pattern); // INVERT THE MATCH OF RE HERE alert(result); return false; } return true; } } <form action="" method="GET" id="form" onsubmit="return jsValidationAndSanitization()"> <fieldset> <legend>Test box</legend> <label for="search" id="search_input">Search</label> <input type="text" id="search_input" name="search_input" /> <input type="submit" id="submit" value="submit" /> </fieldset> </form> So i'm triyng to invert the matches ( only chars and numbers ) of of Javascript pattern matching but actually i didn't find a pretty way to do it and complete the function.
Any suggestions about it ?
return someRegExp.test( submittedInput );I have no idea what you mean with 'inverting the matching' or how that helps you solve this issue. Other ways, outside of JS are to use input type number instead of input type text or using the pattern attribute of inputs to put the regexp for validation there.