简体   繁体   English

JavaScript文本框验证启用按钮

[英]JavaScript textbox validation enable button

Hey so I have two text boxes and a button. 嘿,所以我有两个文本框和一个按钮。 I only wanted the button enabled when the "playerName" textbox contains something and the "upperLimit" textbox contains a integer > 0. I want the button to start disabled then dynamically change as the user is typing in the textbox, constantly checking whats in the textboxes to see if the button should be activated. 我只希望在“ playerName”文本框包含某些内容且“ upperLimit”文本框包含大于0的整数时启用按钮。我希望按钮开始禁用,然后随着用户在文本框中键入内容而动态变化,并不断检查文本框,查看是否应激活该按钮。 Heres what I've tried: 这是我尝试过的:

JavaScript: JavaScript:

var playerNameValid = false;
var upperLimitValid = false;


function validatePlayerName()
{
    if (document.getElementById("initialPlayerNameChoice").Value == "")
    {
        playerNameValid = false;
        document.getElementById("startGameButton").disabled = true;
    }
    else
    {
        playerNameValid = true;

        if (upperLimitValid == true)
        {
            document.getElementById("startGameButton").disabled = false;
        }
    }
}


function validateUpperLimit()
{
    if (isNaN(document.getElementById("initialUpperLimitChoice").valuetextContent))
    {
        upperLimitValid = false;
        document.getElementById("startGameButton").disabled = true;
    }
    else
    {
        upperLimitValid = true;

        if (playerNameValid == true)
        {
            document.getElementById("startGameButton").disabled = false;
        }
    }
}

Markup: 标记:

<asp:Label ID="enterNameLabel" runat="server" Text="Enter your name: "></asp:Label>
<asp:TextBox ID="initialPlayerNameChoice" runat="server"></asp:TextBox>
<br /><br/>
<asp:Label ID="enterUpperLimitLabel" runat="server" Text="Enter upper limit: "></asp:Label>
<asp:TextBox ID="initialUpperLimitChoice" runat="server"></asp:TextBox>
<br /><br />
<asp:Button ID="startGameButton" enabled="false" runat="server" Text="Start Game" />

Code Behind: 背后的代码:

    initialPlayerNameChoice.Attributes.Add("onkeyup", "javascript:validatePlayerName()");
    initialUpperLimitChoice.Attributes.Add("onkeyup", "javascript:validateUpperLimit()");

You've almost got it. 你差不多了。 There are just a few lines you need to fix in your code: 您只需在代码中修正几行即可:

if (document.getElementById("initialPlayerNameChoice").Value == "")
//In JavaScript there is no property called 'Value' -----^  use 'value' instead:
if (document.getElementById("initialPlayerNameChoice").value == "")


if (isNaN(document.getElementById("initialUpperLimitChoice").valuetextContent))
//Again, in JavaScript there is no property called 'valuetextContent' ---^
//Furthermore, 0 is a number, so if you kept this, the button would enable on 0
//You can use a regular expression to make sure it is a number > 0, and
//re-arranging your function eliminates re-checking of logic:
function validateUpperLimit()
{
  var num = document.getElementById("initialUpperLimitChoice").value.match(/^\d+$/);
  if(num && num[0] > 0)
  {
    upperLimitValid = true;

    if (playerNameValid == true)
    {
      document.getElementById("startGameButton").disabled = false;
    }
  }
  else
  {
    upperLimitValid = false;
    document.getElementById("startGameButton").disabled = true;
  }
}

Another solution that you could implement is having just a validate() function, and having both fields call that onkeyup . 您可以实现的另一种解决方案是仅具有validate()函数,并使两个字段都调用该onkeyup If you changed your validation functions to return true or false, then you could eliminate the use of global variables and the logic makes more sense. 如果将验证函数更改为返回true或false,则可以取消使用全局变量,并且逻辑更有意义。

function validate(){
  if(validatePlayerName() && validateUpperLimit())
    document.getElementById('startGameButton').disabled = false;
  else
    document.getElementById('startGameButton').disabled = true;
}

function validatePlayerName(){
  if(document.getElementById('initialPlayerNameChoice').value != '')
    return true;
  else
    return false;
}

function validateUpperLimit(){
  var num = document.getElementById('initialUpperLimitChoice').value.match(/^\d+$/);
  if(num && num[0] > 0)
    return true;
  else
    return false;
}

And then instead for your onkeyup code (I don't think you need the javascript: part but I'm not completely sure so if it barks at you, just throw it back in): 然后代替您的onkeyup代码(我认为您不需要javascript:部分,但我不确定,因此如果它吠叫您,请将其放回去):

initialPlayerNameChoice.Attributes.Add("onkeyup", "validate()");
initialUpperLimitChoice.Attributes.Add("onkeyup", "validate()");

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

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