gpt4 book ai didi

javascript - 无法正确提交表格

转载 作者:行者123 更新时间:2023-12-02 17:28:59 24 4
gpt4 key购买 nike

我是 JavaScript 新手,想自己学习它,我有一个问题,我的警报框没有显示在 verify() 方法的第二个方法中,是我做错了什么还是其他什么事情!请帮助我 提前致谢!我的代码:

<html>
<head>

<script>
function Verify(){
if(!isValidName()){
return false;
}

if(!isValidID()){
return false;
}
}

function isValidName(){
var fnam=document.getElementById('fname').value;
var lnam=document.getElementById('lname').value;
var fcn=fnam.charAt(0);
var lcn=lnam.charAt(0);

if(fnam==""||fnam=="First Name"){
alert("Please Enter First Name!");
return false;
}
else if(lnam==""||lnam=="Last Name"){
alert("Please Enter Last Name!");
return false;
}
else if(fcn!=fcn.toUpperCase()){
alert("Please Use Uppercase for the First Letter in the First Name!");
return false;
}
else if(lcn!=lcn.toUpperCase()){
alert("Please Use Uppercase for the First Letter in the Last Name!");
return false
}
else{
var fc=0;
for(i=0; i<fnam.length; i++){
fc+=1;
if(fnam.charAt(i)>=0||fnam.charAt(i)<=9){
alert("Your First Name Contains an Integer! at index="+fc);
return false;
break;
}
}
var lc=0;
for(j=0; j<lnam.length; j++){
lc+=1;
if(lnam.charAt(j)>=0||lnam.charAt(j)<=9){
alert("Your Last Name Contains an Integer! at index="+lc);
return false;
break;
}
}
}
}

function isValidID(){

var g=document.getElementById('aaa').value;
if(g=="abc"){
alert("SOMETHING WRONG!");
return false;
}
}

function clearLastName(){
document.getElementById('lname').value="";
}

function clearFirstName(){
document.getElementById('fname').value="";
}
</script>
<title>
This is NIC Form Example
</title>
</head>

<body>
<h1>
This is NIC Form Example!
</h1>
<form name="nicform" onsubmit="return Verify()">
<table border="1">
<tr>
<td>
First Name:
</td>
<td>
<input type="text" id="fname" value="First Name" onClick="clearFirstName()">
</td>
</tr>
<tr>
<td>
Last Name:
</td>
<td>
<input type="text" id="lname" value="Last Name" onClick="clearLastName()">
</td>
</tr>
<tr>
<td>
Gender:
</td>
<td rowspan="2">
Male: <input type="radio" id="male" name="rad">
<br/>
Female: <input type="radio" id="female" name="rad">
</td>
</tr>
<tr>
</tr>
<tr>
<td>
<input type="text" id="aaa" value="abc" >
</td>
<td align="center" colspan="2">
<button type="submit">GO</button>
</td>
</tr>
</table>
</form>
</body>
</html>
<小时/>

问题是,如果 id 为“aaa”的字段值保持不变,即如果提交表单时其值为“abc”,则不会显示警报框!

最佳答案

我认为永远不要调用第二个方法,因为如果一切都正确,那么两个函数中都会缺少“返回 true”。因此,如果您添加它,它将解决您的问题。

如果函数一执行正确,那么它必须返回 true。如下所示

    function Verify() {
if (!isValidName()) {
return false;
}

if (!isValidID()) {
return false;
}
}

function isValidName() {
var fnam = document.getElementById('fname').value;
var lnam = document.getElementById('lname').value;
var fcn = fnam.charAt(0);
var lcn = lnam.charAt(0);

if (fnam == "" || fnam == "First Name") {
alert("Please Enter First Name!");
return false;
}
else if (lnam == "" || lnam == "Last Name") {
alert("Please Enter Last Name!");
return false;
}
else if (fcn != fcn.toUpperCase()) {
alert("Please Use Uppercase for the First Letter in the First Name!");
return false;
}
else if (lcn != lcn.toUpperCase()) {
alert("Please Use Uppercase for the First Letter in the Last Name!");
return false
}
else {
var fc = 0;
for (i = 0; i < fnam.length; i++) {
fc += 1;
if (fnam.charAt(i) >= 0 || fnam.charAt(i) <= 9) {
alert("Your First Name Contains an Integer! at index=" + fc);
return false;
break;
}
}
var lc = 0;
for (j = 0; j < lnam.length; j++) {
lc += 1;
if (lnam.charAt(j) >= 0 || lnam.charAt(j) <= 9) {
alert("Your Last Name Contains an Integer! at index=" + lc);
return false;
break;
}
}
}
return true;
}

function isValidID() {

var g = document.getElementById('aaa').value;
if (g == "abc") {
alert("SOMETHING WRONG!");
return false;
}
return true;
}

function clearLastName() {
document.getElementById('lname').value = "";
}

function clearFirstName() {
document.getElementById('fname').value = "";
}

关于javascript - 无法正确提交表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23249922/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com