I want to detect wheather user has entered any whitespace character(\n,\t,\v,\r,etc) in password. I created a function to detect using regex, and call it when user submits the form.
When i fetch the password value, javascript escapes the string escape squences's backslash. I mean it convert \n to \\n, so my regex returns false. Here is my code:
let form = document.querySelector("form")
let inp = document.querySelector("#inp");
const hasWhitespace = (s)=>{
return /\s/.test(s)
};
form.addEventListener('submit',(e)=>{
e.preventDefault()
if(hasWhitespace(inp.value)){
alert("whitespaces not allowed ")
}
})
I have used replaceAll("\\\\","\\")
to replace double backslash into single but it didn't work.
Thanks for your answer
If you're restricting your user from entering any whitespace then you simply use.trim() method
like this let inp = document.querySelector("#inp").trim();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.