3

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.

Zword
  • 6,605
  • 3
  • 27
  • 52

3 Answers3

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
  • how will i specify which textbox to check. –  Oct 23 '12 at 18:45
  • 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