简体   繁体   English

使用javascript验证文本字段中的输入(需要为两个值之一)

[英]Validate input in a text field with javascript (needs to be one of two values)

long time reader, first time poster. 长期的读者,第一次的海报。 I'm relatively new to Javascript, but I've been tinkering with HTML, PHP and SQL for a while now. 我是Java的新手,但是一段时间以来我一直在修补HTML,PHP和SQL。 Anyways, long story short my instructor at college gave us an assignment for some pseudocode, and I decided to see if I could make it into a working form with Javascript. 总之,长话短说,我大学的老师给我们分配了一些伪代码,我决定看看是否可以使用Javascript将其转换为工作形式。 I've already submitted my assignment, don't worry, you aren't helping me cheat. 我已经提交了作业,请放心,您并没有在帮助我作弊。 I just want to see if this can be done in Javascript, as impractical as it might be: 我只想看看这是否可以用Javascript完成,但可能不可行:

There's 3 classic movies on sale this week: 1. Star Wars 2. ET 3. Raiders of the Lost Ark. 本周有3部经典电影在售:1.星球大战2. ET 3.夺宝奇兵》。

The user has to enter the number of the movie they want to see. 用户必须输入他们想要看的电影的编号。 If the value entered is <1 or >3, the page should return an error. 如果输入的值是<1或> 3,则页面应返回错误。

The user has the option to stream or download the movie, and needs to enter in a textbox S for streaming or D for downloading. 用户可以选择流式传输或下载电影,并且需要输入文本框S进行流传输,或输入D进行下载。

So far, what I need help with is validating the input from the streaming/downloading textbox. 到目前为止,我需要帮助的是验证来自流/下载文本框的输入。 This is what I have for my code so far: 到目前为止,我的代码是这样的:

<p id="welcome">Welcome to MovieStream! Would you like to Stream or Download   
your movies? Type S for stream, D for download</p>
<form>
<input type="text" id="purchaseType">
<input type="submit" action="validateForm()">
</form>
<script type="text/javascript">
function validateForm() {

if (document.getElementById("purchaseType").value != S || D) {
    alert("Please type S to stream the movie, or D to download");
}
}
</script>

So far this is as far as I've gotten. 到目前为止,这是我所了解的。 When I click submit, nothing happens (I've even tried 当我单击提交时,什么也没有发生(我什至尝试过

document.getElementById("welcome").innerHTML += "Please type S to stream the movie, or D to Download");

but it still doesn't work. 但它仍然不起作用。 I think it's something with the ".value != S || D" area but I can't seem to find any information anywhere. 我认为这与“ .value!= S || D”区域有关,但是我似乎在任何地方都找不到任何信息。 Is this operation even possible in Javascript alone? 仅使用Javascript还能进行此操作吗? I really hope this makes sense, and someone will hopefully have an answer for me. 我真的希望这是有道理的,希望有人会为我解答。 Thanks :) 谢谢 :)

You can set a pattern attribute to the input element like this: 您可以将pattern属性设置为input元素,如下所示:

  <input type="text" id="purchaseType" pattern="S|D" required>

Then, if you want to customize the error message, check the validity property when an input event is raised on it: 然后,如果要自定义错误消息,请在其上引发input事件时检查validity属性:

purchaseType.addEventListener( "input", function ( event ) 
{
  if ( purchaseType.validity.patternMismatch ) 
    purchaseType.setCustomValidity( "Please type S to stream the movie, or D to download" ) 
  else 
    purchaseType.setCustomValidity( "" )
} )

Finally, add CCS rules with pseudo-element :invalid that will give a visual feedback, for example: 最后,添加带有伪元素:invalid CCS规则,以提供视觉反馈,例如:

#purchaseType:invalid {
  color: red ;
  border: 1px solid  red ;
  box-shadow: 0 0 10px 0 red  ;
  }

#purchaseType:valid {
  color: limegreen ;
  border: 1px solid limegreen ;
  }

Reference: MDN Data Form Validation 参考: MDN数据表单验证

 purchaseType.addEventListener( "input", function ( event ) { if ( purchaseType.validity.patternMismatch ) purchaseType.setCustomValidity( "Please type S to stream the movie, or D to download" ) else purchaseType.setCustomValidity( "" ) } ) 
 #purchaseType:invalid { color: red ; border: 1px solid red ; box-shadow: 0 0 10px 0 red ; } #purchaseType:valid { color: limegreen ; border: 1px solid limegreen ; box-shadow: 0 0 5px 0 limegreen; } #purchaseType:focus { outline: none ; } 
 <p id="welcome">Welcome to MovieStream! Would you like to Stream or Download your movies? Type S for stream, D for download</p> <form> <input type="text" id="purchaseType" pattern="S|D" required placeholder="Type S or D" autofocus> <input type="submit"> </form> 

You need to compare the input with wished values as strings: 您需要将输入的期望值与字符串进行比较:

if (document.getElementById("purchaseType").value != 'S' && document.getElementById("purchaseType").value != 'D') {

(the single quotes) (单引号)

UPDATE: 更新:

Here are the parts you need to change: 这是您需要更改的部分:

  • to prevent sending the form to the server you need the onsubmit callback 为了防止将表单发送到服务器,您需要onsubmit回调
  • replace the #error contents instead of appending the (same) error message after each click 每次点击后,替换#error内容,而不是附加(相同)错误消息

 function validateForm() { if (document.getElementById("purchaseType").value != 'S' && document.getElementById("purchaseType").value != 'D') { document.getElementById("error").innerHTML = "Please type S to stream the movie, or D to download"; return false; } return true; } 
 <form onsubmit="return validateForm()"> <input type="text" id="purchaseType"> <input type="submit"> </form> <p id="error"></p> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM