I am trying to detect spaces in the text field using JavaScript, so whenever there is a space in the text field an alert should pop up, but this code is not working, it 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 are not allowed");
}
}
</script>
<body onload="detectSpace()">
<form action="demo_form.asp">
First name: <input type="text" name="FirstName" value=""><br>
Last name: <input type="text" name="LastName" value=""><br>
<input type="submit" value="Submit">
</form>
<p>Click the "Submit" button and the form-data will be sent to a page on the server called "demo_form.asp".</p>
</body>
</html>