在点击提交按钮时,如果在输入字段中没有输入特定数字,如何发出警告。

huangapple go评论113阅读模式
英文:

how to give an alert if specific number is not written in input field on clicking submit button

问题

我有一个输入字段,我希望用户输入一个预定的数字,例如500,如果用户点击提交按钮并且输入的值不是500,则应该有一个JavaScript警告。如果用户确实输入了500然后点击提交按钮,那么就不应该发生警告。

<input name="number" type="text" size="36" maxlength="60" />
<input name="Submit" type="submit" value="submit" />
英文:

I have an input field and I want the user to put in a number that is predetermined, like 500, if the user clicks the submit button and value they typed in is NOT 500, there should be a javascript alert. If the user does type in 500 and then clicks the submit button then alert should not happen.

&lt;input name=&quot;number&quot; type=&quot;text&quot; size=&quot;36&quot; maxlength=&quot;60&quot; /&gt;
&lt;input name=&quot;Submit&quot; type=&quot;submit&quot; value=&quot;submit&quot;  /&gt;

答案1

得分: 1

我建议使用ID而不是带有提交按钮的表单,因为你将不得不每次重新加载页面并读取URL中的GET参数。

改用onclick,就像这个例子:

<html>
	<head>
		<script>
			function myFunction() {
				if (document.getElementById("number").value != "500")
					alert("Your alert message");
			}
		</script>
	</head>
	<body>
		<input id="number" type="text" size="36" maxlength="60" />
		<input type="button" value="Submit" onclick="myFunction()" />
	</body>
</html>
英文:

I would recommend to use ids instead of a form with submit because you will have to reload the page everytime and read the GET parameters out of the URL.

Use onclick instead, like this example:

&lt;html&gt;
	&lt;head&gt;
		&lt;script&gt;
			function myFunction() {
				if (document.getElementById(&quot;number&quot;).value != &quot;500&quot;)
					alert(&quot;Your alertmessage&quot;);
			}
		&lt;/script&gt;
	&lt;/head&gt;&lt;body&gt;
		&lt;input id=&quot;number&quot; type=&quot;text&quot; size=&quot;36&quot; maxlength=&quot;60&quot; /&gt;
		&lt;input type=&quot;button&quot; value=&quot;Submit&quot; onclick=&quot;myFunction()&quot; /&gt;
	&lt;/body&gt;
&lt;/html&gt;

答案2

得分: 0

我建议您使用 onclick 处理程序而不是提交按钮的提交处理程序,并为您的输入字段分配一个 id。onclick 函数将评估您的输入值。您可以解析输入值以将其转换为整数,并将其与 500 进行比较。

或者,您可以忽略 parseInt 部分,将值与 "500" 进行比较,以您喜欢的方式进行比较。

如果值通过条件,然后将该值传递给您的提交功能。

const checkNum = () => {
  if (parseInt(document.getElementById('thisNum').value) !== 500) {
    alert("NOT NUMBER")
  } else {
    submitFunction(document.getElementById('thisNum').value)
  }
}

const submitFunction = (x) => {
  console.log(x)
  //your submit functionality
}
<input id="thisNum" name="number" type="text" size="36" maxlength="60" />
<input name="Submit" type="submit" value="Submit" onclick="checkNum()" />
英文:

I would recommend using onclick handler rather than a submit handler for your submit button and assign an id to your input. The onclick function will evaluate your input's value. You can parse the input value to make it an integer and check it against 500.

Or, you can ignore the parseInt part and compare the value to "500". Whichever you prefer.

If the value passes the conditional, then pass that value to your submit functionality.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const checkNum = () =&gt; {
  if (parseInt(document.getElementById(&#39;thisNum&#39;).value) !== 500) {
    alert(&quot;NOT NUMBER&quot;)
  } else {
    submitFunction(document.getElementById(&#39;thisNum&#39;).value)
  }
}

const submitFunction = (x) =&gt; {
  console.log(x)
  //your submit functionaliy
}

<!-- language: lang-html -->

&lt;input id=&quot;thisNum&quot; name=&quot;number&quot; type=&quot;text&quot; size=&quot;36&quot; maxlength=&quot;60&quot; /&gt;
&lt;input name=&quot;Submit&quot; type=&quot;submit&quot; value=&quot;Submit&quot; onclick=&quot;checkNum()&quot; /&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月4日 00:21:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581901.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定