knutsford Posted June 16, 2016 Share Posted June 16, 2016 If you use <input required> how do you stop it from allowing just spaces and how do you make sure there is a minimum entry of 8 non spaces. Thanks Link to comment Share on other sites More sharing options...
davej Posted June 16, 2016 Share Posted June 16, 2016 For that you'll need Javascript. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"/> <title>title</title> <script> window.onerror = function(a,b,c){ alert('Javascript Error: '+a+'\nFile: '+b+'\nLine: '+c); return true; } </script> <script> 'use strict'; function validateForm(evt){ var txt = document.getElementById('txt1').value.trim(); if (txt.length < 8){ evt.preventDefault(); alert('Submission Aborted\nText is only ' + txt.length + ' characters'); } } window.onload = function(){ document.getElementById('form1').onsubmit = validateForm; } </script> </head> <body> <form id="form1"> <input type="text" name="txt1" id="txt1"/> <input type="submit" value="Submit"/> </form> </body> </html> Link to comment Share on other sites More sharing options...
Ingolme Posted June 16, 2016 Share Posted June 16, 2016 You can use the pattern attribute: <input required pattern="\s*\S{8,}\s*"> Link to comment Share on other sites More sharing options...
davej Posted June 16, 2016 Share Posted June 16, 2016 Oh, I didn't know that. Nice! http://www.w3schools.com/tags/att_input_pattern.asp Link to comment Share on other sites More sharing options...
knutsford Posted June 16, 2016 Author Share Posted June 16, 2016 Thanks. I can do it with JavaScript without a problem. I just thought I would try required instead but I had no idea what to use for the pattern Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now