Javascript 将动态文本字段设置为一定数量的字符

Javascript 将动态文本字段设置为一定数量的字符,javascript,php,forms,validation,input,Javascript,Php,Forms,Validation,Input,如何设置为填写表单动态文本字段,输入指定的字符数不少于或不多于,例如12 <script> var input = document.getElementById( 'myFormId' )['exactInput'] , lastCorrectValue = '' , neededSize = 12 input.addEventListener( 'change', function ( e ) { if (

如何设置为填写表单动态文本字段,输入指定的字符数不少于或不多于,例如12

  <script>
    var input = document.getElementById( 'myFormId' )['exactInput']
        , lastCorrectValue = ''
        , neededSize = 12

    input.addEventListener( 'change', function ( e )
    {
      if ( e.target.value.length !== neededSize )
      {
        e.target.value = lastCorrectValue       
      }
      else
      {
        lastCorrectValue = e.target.value        
      }
    } )
  </script>
</body>
maxlength=“12”将数字限制为12,但不超过12

  <script>
    var input = document.getElementById( 'myFormId' )['exactInput']
        , lastCorrectValue = ''
        , neededSize = 12

    input.addEventListener( 'change', function ( e )
    {
      if ( e.target.value.length !== neededSize )
      {
        e.target.value = lastCorrectValue       
      }
      else
      {
        lastCorrectValue = e.target.value        
      }
    } )
  </script>
</body>
下面的脚本代码,我将以这种方式保护它。这可以直接用php而不是JavaScript完成,因为您可以关闭它吗

<script src="js/addInput.js" language="Javascript" type="text/javascript"></script>
<div id="dynamicInput">Serial No<b><font color="#FF0000">*</font></b> 
1: <input type="text" name="myInputs[]" maxlength="12" > np 6544/2014-01 
</div>
<input type="button" value="Add number" onClick="addInput('dynamicInput');">
  <script>
    var input = document.getElementById( 'myFormId' )['exactInput']
        , lastCorrectValue = ''
        , neededSize = 12

    input.addEventListener( 'change', function ( e )
    {
      if ( e.target.value.length !== neededSize )
      {
        e.target.value = lastCorrectValue       
      }
      else
      {
        lastCorrectValue = e.target.value        
      }
    } )
  </script>
</body>

序列号*
1:np 6544/2014-01
kod pliku js/addInput.js

var counter = 1;
var limit = 20;
function addInput(divName){
  if (counter == limit) {
    alert("Add only 20 numbers");
  }
  else {
    var newdiv = document.createElement('div');
    newdiv.innerHTML = "Serial number<font color='#FF0000'>*</font> " + (counter + 1) + ": <input type='text' maxlength='12' name='myInputs[]'>";
    document.getElementById(divName).appendChild(newdiv);
    counter++;
  }
}
  <script>
    var input = document.getElementById( 'myFormId' )['exactInput']
        , lastCorrectValue = ''
        , neededSize = 12

    input.addEventListener( 'change', function ( e )
    {
      if ( e.target.value.length !== neededSize )
      {
        e.target.value = lastCorrectValue       
      }
      else
      {
        lastCorrectValue = e.target.value        
      }
    } )
  </script>
</body>
var计数器=1;
风险价值限额=20;
函数addInput(divName){
如果(计数器==限制){
警报(“仅添加20个数字”);
}
否则{
var newdiv=document.createElement('div');
newdiv.innerHTML=“序列号*”+(计数器+1)+“:”;
document.getElementById(divName).appendChild(newdiv);
计数器++;
}
}
在输入中使用maxlength

<input type="text" name="usrname" maxlength="20">
  <script>
    var input = document.getElementById( 'myFormId' )['exactInput']
        , lastCorrectValue = ''
        , neededSize = 12

    input.addEventListener( 'change', function ( e )
    {
      if ( e.target.value.length !== neededSize )
      {
        e.target.value = lastCorrectValue       
      }
      else
      {
        lastCorrectValue = e.target.value        
      }
    } )
  </script>
</body>

要将输入限制在12到12之间,请尝试以下操作

<input type='text' pattern=".{12,12}" maxlength="12" name='myInputs[]'/>
  <script>
    var input = document.getElementById( 'myFormId' )['exactInput']
        , lastCorrectValue = ''
        , neededSize = 12

    input.addEventListener( 'change', function ( e )
    {
      if ( e.target.value.length !== neededSize )
      {
        e.target.value = lastCorrectValue       
      }
      else
      {
        lastCorrectValue = e.target.value        
      }
    } )
  </script>
</body>

我认为在浏览器中避免使用JS是个坏主意,因为有人可以关闭它:

  <script>
    var input = document.getElementById( 'myFormId' )['exactInput']
        , lastCorrectValue = ''
        , neededSize = 12

    input.addEventListener( 'change', function ( e )
    {
      if ( e.target.value.length !== neededSize )
      {
        e.target.value = lastCorrectValue       
      }
      else
      {
        lastCorrectValue = e.target.value        
      }
    } )
  </script>
</body>
  • 一个页面会丢失很多动态和交互的东西(我认为现在这是毫无意义的)
  • 客户端浏览器可以代替服务器完成所有脏活
  • 即使禁用了JS,用户也可以手动向服务器发送带有错误格式参数的GET请求
  • 获得良好效果的一个常见做法是使用JS(通过处理用户活动防止输入错误数据)在浏览器中进行响应良好的设计,并在服务器上重新验证一些请求参数(这些参数对某些格式非常敏感)
因此,请在浏览器中尝试以下操作:

  <script>
    var input = document.getElementById( 'myFormId' )['exactInput']
        , lastCorrectValue = ''
        , neededSize = 12

    input.addEventListener( 'change', function ( e )
    {
      if ( e.target.value.length !== neededSize )
      {
        e.target.value = lastCorrectValue       
      }
      else
      {
        lastCorrectValue = e.target.value        
      }
    } )
  </script>
</body>

var input=document.getElementById('myFormId')['exactInput']
,lastCorrectValue=“”
,neededSize=12
input.addEventListener('change',函数(e)
{
if(e.target.value.length!==neededSize)
{
e、 target.value=lastCorrectValue
}
其他的
{
lastCorrectValue=e.target.value
}
} )
当输入失去焦点时,如果新数据的长度错误,它将放弃更改

  <script>
    var input = document.getElementById( 'myFormId' )['exactInput']
        , lastCorrectValue = ''
        , neededSize = 12

    input.addEventListener( 'change', function ( e )
    {
      if ( e.target.value.length !== neededSize )
      {
        e.target.value = lastCorrectValue       
      }
      else
      {
        lastCorrectValue = e.target.value        
      }
    } )
  </script>
</body>

'change'
事件在旧浏览器中可能有缺陷,因此您也可以用同样的方法使用
'blur'
事件。

顺便提一下,op说:
这可以直接用php而不是JavaScript完成,因为您可以关闭它吗?
  <script>
    var input = document.getElementById( 'myFormId' )['exactInput']
        , lastCorrectValue = ''
        , neededSize = 12

    input.addEventListener( 'change', function ( e )
    {
      if ( e.target.value.length !== neededSize )
      {
        e.target.value = lastCorrectValue       
      }
      else
      {
        lastCorrectValue = e.target.value        
      }
    } )
  </script>
</body>