Is there a way in HTML5 to set the pattern attribute to not accept inputs containing backslash?

StackOverflow https://stackoverflow.com/questions/18315338

  •  25-06-2022
  •  | 
  •  

Question

I have a file directory location field and because it gets saved as a link the backlashes the exist in copied directory paths must be changed to forward slashes. I need to set a notification to change these for users that do not do so. I would like to use html5 built in feature for formatting inputs such as 'pattern' but I have been unable to figure out how to do so. Any help will be appreciated.

Was it helpful?

Solution

this should work:

<form method="POST">
    <input pattern="[^\\]+" name="pattern" value="" title="no backslash allowed">
    <input type="submit">
</form>

live example: http://jsfiddle.net/cNWDM/1/

change [^\\]+ to [^\\]* if you want to allow an empty field

still, i would strongly recommend that you check / convert the slashes server side, client side is good for usability but never safe

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top