防止数值用户输入文本框

时间:2019-07-09 10:34:14

标签: javascript html hta

旨在防止用户将数字输入到给定的输入框中。我特别希望防止用户在“名字”文本字段中输入数字。

<html>
  <head>
    <title>COI Request</title>

<HTA:APPLICATION ID="menu" 
APPLICATIONNAME="menu" 
BORDER="thin"
BORDERSTYLE="dialog"
CAPTION="Yes"k
CONTEXTMENU="Yes"
NAVIGABLE="Yes"
INNERBORDER="no"
MAXIMIZEBUTTON="No"
MINIMIZEBUTTON="No"
SCROLL="no" />

     <script>
      window.onload = function sizeIt(){
      var sw = (screen.width-775)/2;
      var sh = (screen.height-650)/2;
      window.moveTo(sw,sh);window.resizeTo(775,650)}
     </script>
  </head>
  <body style = "background: url(https://static.turbosquid.com/Preview/001163/407/D5/presentations-background-3D-model_0.jpg)">
  <br><br>
   <div style="justify-content: center; text-align:center;">
   <img " src="https://www.jobsexpo.ie/wp-content/uploads/2018/11/Eli-Lilly.png" alt="Eli Lilly" width="600" height="150">
   </div>
   <br>
    <h1 style="font-size:60px; text-align:center; color:red; text-decoration:underline; font-weight:bold">COI Requests:</h1>


    <script language='javascript' >
        function pipeText() {
            var CustomerID=document.getElementById('CustomerID').value;
            var FirstName=document.getElementById('FirstName').value;
            var LastName=document.getElementById('LastName').value;

            var Batch = new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1);
            close(Batch.WriteLine('"'+CustomerID+'","'+FirstName+'","'+LastName+'"'));
      }
    </script>

 <div style="font-family:Georgia; color:red; font-size:20px; display:flex; flex-direction: row; justify-content: center; text-align:center;"> 
    <label style="font-size:30px" for="Student">Customer ID:</label>
    <input type='text' name='CustomerID' size='25' placeholder="Enter Password"></input><br>
 </div>

 <div style="font-family:Georgia; color:red; display:flex; flex-direction: row; justify-content: center; text-align:center;"> 
    <label style="font-size:30px" for="Student">First Name:</label>
    <input type='text' name='FirstName' size='25'></input><br>
 </div>

 <div style="font-family:Georgia; color:red; display:flex; flex-direction: row; justify-content: center; text-align:center;"> 
    <label style="font-size:30px" for="Student">Last Name:</label>
    <input type='text' name='LastName' size='25'></input><br>
 </div>

<br>
 <div style="justify-content: center; text-align:center;">
    <button onclick="self.close()">Close</button> &nbsp   
    <button onclick='pipeText()'">Submit</button>
 </div>
  </body>
</html>

寻找要防止在“名字”输入框中输入数字的值

2 个答案:

答案 0 :(得分:1)

我的问题的解决方案是:

JavaScript:

<script language='javascript' >
        function pipeText() {
            var CustomerID=document.getElementById('CustomerID').value;
            var FirstName=document.getElementById('FirstName').value;
            var LastName=document.getElementById('LastName').value;

            var Batch = new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1);
            close(Batch.WriteLine('"'+CustomerID+'","'+FirstName+'","'+LastName+'"'));
      }

 function validateKeyStrokes(event) {
    var charCode = (event.which) ? event.which : event.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return true;
    }
    return false;
}

HTML:

<div style="font-family:Georgia; color:red; display:flex; flex-direction: row; justify-content: center; text-align:center;"> 
    <label style="font-size:30px" for="Student">First Name:</label>
    <input type='text' name='FirstName' onkeypress="return validateKeyStrokes(event)" size='25'></input><br>
 </div>

答案 1 :(得分:-1)

您可以将输入的type属性与模式一起使用:

 <input type="text" name="name" id="name" required pattern="[A-Za-z]+">