html - JavaScript code does not work -



html - JavaScript code does not work -

i trying observe spaces in text field using javascript, whenever there space in text field alert should pop up, code not working, should work on both text fields.

<!doctype html> <html> <script type="text/javascript"> function detectspace() { $returnvalue = preg_match('/[^a-z^a-z^0-9]/', $str, $matches); if ($returnvalue==1) { alert("spaces & symbols not allowed"); } } </script> <body onload="detectspace()"> <form action="demo_form.asp"> first name: <input type="text" name="firstname" value=""><br> lastly name: <input type="text" name="lastname" value=""><br> <input type="submit" value="submit"> </form> <p>click "submit" button , form-data sent page on server called "demo_form.asp".</p> </body> </html>

preg_match php function , not javascript function - made more obscure fact back-end code asp! php , javascript different languages. match on string in javascript need alter code to:

function detectspace(str) { var look = new regexp(/\s/); var returnvalue = expression.test(str); if (returnvalue === true) alert("spaces & symbols not allowed"); }

with you'll need pass in value want test against argument in detectspace function:

detectspace("foo"); // no alert fired detectspace("foo bar"); // alert fired

note i've changed regular look /\s/ - matches white space , homecoming true if spaces found.

javascript html

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -