所以我发现了这个,它似乎工作正常并且非常有帮助。我只需要知道代码“!= -1”做什么。没有它,代码似乎无法工作。
<html>
<head>
<script language="javascript">
function validate_input(data){
var iChars = "!@#$%^&*()+=-[]\\;,./{}|:<>?~_"; /* characters not allowed */
for (var i = 0; i < data.length; i++) {
if (iChars.indexOf(data.charAt(i)) != -1) {
alert ("Your filename has special characters. \nThese are not allowed.");
return false;
}
}
if(data.length < 1){
alert("File Name is required");
return false;
}
}
</script>
</head>
<body>
<form action="#" method="Get" onSubmit="return validate_input(document.getElementById('filename').value);">
<input type="text" id="filename" name="filename">
<input type="submit">
</form>
</body>
</html>