Javascript名称验证 [英] Javascript name validation

查看:142
本文介绍了Javascript名称验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个javascript函数来验证表单上的字段。该功能应该确保该字段不为空,不超过35个字符的限制,并且只包含字母字符和连字符( - )。我有代码,以确保该字段不是空的,并且它不超过35个字符工作正常,但我添加了验证字段的可用字符的代码,我通过将字段留空以确保仍然有效但是当我点击提交按钮该功能似乎并没有验证,没有给我一个警报,只是提交。这里是我的代码:

  function validateFamily()
{
var family = document.getElementById('家庭');
var stringf = document.getElementById('family')。value;
if(family.value ==)
{
alert(姓氏必须填写);
返回false;

else if(document.getElementById('family').value.length> 35)
{
alert(姓氏不能超过35个字符) ;
返回false;

else if(/ [^ a-zA-Z\-\] /。test(stringf))
{
alert(姓氏只能包含字母数字字符和hypehns( - ))
返回false;
}
返回true;
}

< form name =eoiformmethod =POSTaction =<?php echo $ _SERVER [PHP_SELF];?> id =eoionsubmit =return validateFamily()&&& validateGiven()&& validateMaleFemale()&& amp;& validDate()&& validateAddress()&& amp; validatePost()& ; validateWork()&& validateHome()&& validateMob()&& validateCheckBoxes();>

< b>学生的姓氏< / b>
< br>
< input type =textid =familyname =family/><?php echo $ strmsgf; ?>

< input type =submitname =submitid =submitvalue =submit/>

< / form>

任何人都可以告诉我如何修正我的代码?

解决方案

更新:



对不起,问题在于你的正则表达式,我错过了, :

  var ck_password = / ^ [A-Za-z0-9  - ] /; 
$ b $ if(!ck_password.test(stringf))
{

alert(姓氏只能包含字母和数字字符( - ))

}

控制台在chrome中,转到右上角的选项,选择TOOLS,然后选择DEVELOPER TOOLS。

I have a javascript function written to validate a field on my form. This function is supposed to make sure the field is not empty, does not exceed the limit of 35 characters and only contains alphabetic characters and a hyphen(-). I had code to make sure the field is not empty and that it does not exceed 35 characters which worked fine but i added code to validate the field to the usable characters and i tested it out by leaving the field empty to make sure that still worked but when i hit the submit button the function didn't seem to validate at all, didn't give me an alert and just submitted. Here is my code:

function validateFamily()
{
var family=document.getElementById('family');
var stringf = document.getElementById('family').value;
if (family.value=="")
    {
     alert("Family name must be filled out");
     return false;
    }
else if (document.getElementById('family').value.length > 35)
    {
        alert("Family name cannot be more than 35 characters");
        return false;
    }
else if (/[^a-zA-Z\-\]/.test( stringf ))
    {
        alert("Family name can only contain alphanumeric characters and hypehns(-)")
        return false;
    }
    return true;
}

<form name="eoiform" method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>" id="eoi" onsubmit="return validateFamily() && validateGiven() && validateMaleFemale() && validDate() && validateAddress() && validatePost() && validateParent() && validateWork() && validateHome() && validateMob() && validateCheckBoxes();">

<b>Student's Family Name</b> 
<br>
<input type="text" id="family" name="family" /><?php echo $strmsgf; ?>

<input type="submit" name="submit" id="submit" value="submit" />

</form>

Could anyone show me how to fix my code?

解决方案

UPDATED:

Sorry, the problem is with your regex, i missed that, change to this its fully working:

  var ck_password =  /^[A-Za-z0-9-]/;

            if(!ck_password.test(stringf))
            {

                alert("Family name can only contain alphanumeric characters and hypehns(-)")

            }

Console in chrome, go to the OPTIONS in the right top coner, select TOOLS, then DEVELOPER TOOLS.

这篇关于Javascript名称验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆