从 HTML 代码主体调用 js 方法,并采用 VARIABLE for 方法



我有这个html代码:

<html>
<title> Upload Malware File </title>
<body>
<header id = "header">
<h1 align="center">Upload Malware File</h1>
<p align="center"> Please choose the malware file you want to upload.</p>
</header>

<form method="post" enctype="multipart/form-data" action="malware_upload.php">
Select malware file to upload:
<input type="file" name="file"required>
<form method="post" enctype="multipart/form-data">
<div >
<label for="mname"><b>Malware Name</b></label>
<input type="text" placeholder="Malware Name" name="mname" <required> *HERE*
</div>
<div >
<label for="uname"><b>Admin Username</b></label>
<input type="text" placeholder="Enter Username" name="uname" required>
</div>
<div>
<label for="psw"><b>Admin Password</b></label>
<input type="password" placeholder="Enter Password" name="psw" required>
</div>
<input type="submit" value="Upload" name="submit">
</form>
</form>
<form>
<input type="submit" formaction="file_upload.php" value="Go to File Uploads" />
<input type="submit" formaction="CreateAdmin.php" name="SI" value="Sign In" />
</form>
<form method="post" enctype="multipart/form-data">
<input type="submit" name = "logout" value="Logout" /> 
</form>

现在我有这个方法:

function checkMalwareName(string)
{
return ("/^[a-zA-Z0-9]+$/").test(string)
}

我如何能够干净地调用 html 代码中">HERE"标记处的 checkMalwareName,如果它为 false,则返回错误?

你必须将函数绑定到元素的事件(oninput(:

改变

<input type="text" placeholder="Malware Name" name="mname" <required>

<input type="text" placeholder="Malware Name" name="mname" oninput="checkMalwareName(this.value)" required >

相关内容

最新更新