简体   繁体   English

javascript文本验证无效。 警报消息不显示

[英]javascript text validation not working. alert message doesn't show

I have a simple form validation script: 我有一个简单的表单验证脚本:

<script language=”javascript”>
    function return validate_form(register)
 {      
if (""==document.forms.register.FNAME.value){
    alert("This field is required!");
    document.forms.register.FNAME.focus();
    return false;
}    
if (""==document.forms.register.LNAME.value){
    alert("This field is required!");
    document.forms.register.LNAME.focus();
    return false;
}
if (EMAIL.value.search( /^[a-zA-Z]+([_\.-]?[a-zA-Z0-9]+)*@[a-zA-Z0-9]+([\.-]?[a-zA-Z0-9]+)*(\.[a-zA-Z]{2,4})+$/ ) == -1){
    alert(“Wrong email”);
    return false;
}       
if('0'==document.forms.register.GENDER.value){
    alert("You must select an option!");
    document.forms.register.GENDER.focus();
    return false;
}
if (""==document.forms.register.ADDRESS.value){
    alert("This field is required!");
    document.forms.register.ADDRESS.focus();
    return false;
}
if (""==document.forms.register.CONTACTNO.value){
    alert("This field is required!");
    document.forms.register.CONTACTNO.focus();
    return false;
}
}
</script>

the function is called using the onSubmit handler, but nothing happens when submit is clicked. 使用onSubmit处理函数调用该函数,但是单击Submit时没有任何反应。 It goes directly to the PHP script instead of javascript 'intercepting' it. 它直接进入PHP脚本,而不是通过JavaScript“拦截”它。 Any thoughts? 有什么想法吗?

Form HTML: 表单HTML:

 <form name="register" action="register.php" method="POST" onsubmit="return validate_form(register);">
<table width="100%" border="0">
<tr>
  <td width="46%" height="24" align="right">First Name:</td>
  <td width="54%"><input name="FNAME" type="text" size"20" /></td>
</tr>
<tr>
  <td height="24" align="right">Last Name:</td>
  <td><input name="LNAME" type="text" size"20" /></td>
</tr>
<tr>
  <td height="24" align="right">Email Address</td>
  <td><input name="EMAIL" type="text" size"20" /></td>
</tr>
<tr>
  <td height="24" align="right">Gender:</td>
  <td><select name="GENDER">
      <option value="" selected="selected">- Select One -</option>
      <option value="Male">Male</option>
      <option value="Female">Female</option></select></td>
</tr>
<tr>
  <td height="24" align="right">Address:</td>
  <td><input name="ADDRESS" type="text" size"20" /></td>
</tr>
    <tr>
  <td height="24" align="right">Contact No.:</td>
  <td><input name="CONTACTNO" type="text" size"20" /></td>
</tr>
<tr>
  <td height="24" align="right">Password</td>
  <td><input name="PASSWORD" type="password" size"20" /></td>
</tr>
 <tr>
  <td height="24" align="right">Re-type Password</td>
  <td><input name="PASSWORD2" type="password" size"20" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name="submit" type="submit" value="Register" /></td>
</tr>
</table>

</form>

the alert message doesn't show? 警报消息没有显示? what is wrong?? 怎么了??

Well, I refactored your code a bit to make more sense and maintainability 好吧,我对您的代码进行了一些重构,以使其更具意义和可维护性

html change: html更改:

<form name="register" action="register.php" method="POST" onsubmit="return validate_form();">

Sample JS: 样本JS:

function validate_form() {
    var frm = document.forms.register;
    function focus_and_false(el) {
        el.focus();
        return false;
    }
    if( frm.FNAME.value === "" ) { /* repeat this for all form elements you want to validate */
        alert("This field is required!");
        return focus_and_false(frm.FNAME); /* this is not the best, but i'm showing you how you can reduce overall code by not rewriting the same things. */
    }
    return true;
}​

and here is a sample 这是一个样本

You have a HUGE syntax error in your javascript ... 您的JavaScript语法有误...

function return validate_form(register)

should be 应该

function validate_form(register)

Maybe you can start with that :) 也许你可以从这里开始:)

And add an alert just after the function starts so you know that when you call it, it actually executes or at least tries to. 并且在函数启动后立即添加警报,以便您知道在调用该函数时,它实际上会执行或至少会尝试执行。

Another syntax error: 另一个语法错误:

<script language=”javascript”>

Should be 应该

<script type="text/javascript">

And finally i would change the function name to 最后我将函数名称更改为

function validateForm(register)

Let me know if it works :) 让我知道它是否有效:)

You got curly quotes in your code 您的代码中有双引号

alert(“Wrong email”);  <---bad quotes

You should not just use a name 你不应该只使用一个名字

onsubmit="return validate_form(register);"

pass in this which points to the current scope which is the form element. 传入指向当前范围的表单元素。

onsubmit="return validate_form(this);"

What is EMAIL? 什么是电子邮件?

EMAIL.value

Do not just reference element names. 不要只引用元素名称。

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

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