I want a script which should alert the user if space is entered in the text field.I think it requires javascript.I am writing a script in which i don't want user to enter space in the text fields.
Asked
Active
Viewed 6,462 times
3
-
1what have you done so far? what are you stuck on? – jbabey Oct 23 '12 at 18:42
-
In the registration form which i have created i dont want username to be entered with spaces – Oct 23 '12 at 18:43
3 Answers
1
The following function worked
$returnValue = preg_match('/[^a-z^A-Z^0-9]/', $str, $matches);
if ($returnValue==1)
{
alert("spaces & symbols are not allowed");
}
Note: It not only restricts spaces but also restricts symbols.
0
use the match()
method of strings
<script type="text/javascript">
function checkForm()
{
var textb=document.getElementById("textbox1").value;
if(textb.match(' ')){
alert('Spaces not allowed!');
return false;
}
}

Ravindra Bagale
- 17,226
- 9
- 43
- 70
-
-
Fatal error: Call to undefined function match() is shown. Can You specify where should i write this script. – Oct 23 '12 at 18:51
0
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready( function() {
$("textarea").keypress( function(e) {
if (e.which==32){
alert("spaces are not allowed");
}
});
});
</script>
<textarea></textarea>

Heart
- 508
- 1
- 3
- 14
-
man it alerts that spaces are not allowed but space is included in textarea :( – Heart Oct 24 '12 at 05:12