Jump to content

no dots in input


sufian

Recommended Posts

hey I need a script for no dots so when some one fills in the input filed they are not able to put in dots.

 

I have found one that removes spaces but not one that removes dots.

 

remove spaces:

<script type="text/javascript">
function nospaces(t){
if(t.value.match(/\s/g)){
alert('Sorry, you are not allowed to enter any spaces');
t.value=t.value.replace(/\s/g,'');
}
}
</script>

 

 

                       

<input class="input mediumfield" name="username" onkeyup="nospaces(this)" type="text" value="" />

 

 

 

 

 

 

 

 

 

 

 

:php_tags:

 

Link to comment
https://forums.phpfreaks.com/topic/167692-no-dots-in-input/
Share on other sites

Well, no matter what you do you also need to validate on the server page that processes the input.

 

You can handle this in different ways. Personally I don't like scripts that validate on every keystroke. Instead I will validate onblur or on submission of the form. But, to each his own. The following function should work fine.

 

<html>
<head>
<script language="JavaScript" type="text/javascript">
  function hasPeriods(value)
  {
    return value.match(/\./);
  }

  function checkDot(fieldObj)
  {
    if (hasPeriods(fieldObj.value))
    {
        alert('Periods are not allowed.');
        fieldObj.value = fieldObj.value.replace('.', '');
    }
    return;
  }
  </script>
  </head>

  <body>
    <input type="text" name="test_field" onkeyup="checkDot(this);" />
  </body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/167692-no-dots-in-input/#findComment-884435
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.