Only allow certain characters to be entered in html textinput

怎甘沉沦 提交于 2019-11-27 15:03:29
Ankit Dhadse

You can try in your input text as below:

<input type="text" pattern="[a-zA-Z0-9!@#$%^*_|]{6,25}" />

So the code changes look like below:

    <form action="#" method="get">
       User Name:<br />
       <input type="text" pattern="[a-zA-Z0-9!@#$%^*_|]{6,25}" /><br />
       Password:<br />
       <input type="password" /><br />
       <input type="submit" value="Log In" /> 
   </form>

this will do it without using JavaScript. pattern can be used instead.

It is rather effective than JavaScript for form validation.

use this

<script language="javascript">
document.logOn.onsubmit=validate;

function validate(){

var name=document.logOn.pw.value;
var reg=/[^a-zA-Z0-9\!\@\#\$\%\^\*\_\|]+/;
if(reg.test(name)){              
alert("Your Password Cant Have any thing other than a-zA-Z0-9!@#$%^*_| - Play It    Straight!");
return false;
}               

return true;
}

</script>

This should do

<input type="text" name="something" pattern="[a-zA-Z0-9!@#$%^*_|]{0,100}">

Try this...

if(!/[a-zA-Z0-9!@#$%^*_|]./.test(name)){    

Try this and revert me back if it works for you

function validate(){
        var name=document.logOn.pw.value;
        var test = new RegExp("[a-zA-Z0-9!@#$%^*_|]");
        if(!name.match(test)){              
           alert("Your Password Cant Have any thing other than a-zA-Z0-9!@#$%^*_| - Play It                     Straight!");
        return false;
  }               
   return true;
}

http://jsfiddle.net/KJEcG/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!