表格验证帮助 [英] Form Validation help

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

问题描述

你好,


我对asp和jscript相当新,我现在正在学习某些表单验证的过程中获得
。我一步一步地通过

验证每个字段并测试它,然后再转到下一个字段

确保我是正确的。我在验证时遇到了问题,如果我在代码中添加了一个其他的代码。


这是我试图做的:Form(ITTermination )有一个字段

(EmployeeName),我想验证以检查没有值和

字母字符A - Z,没有数字。


此代码仅适用于验证字段中的无值:


< script language =" JavaScript">


<! -


函数validate_form()

{

valid = true;


if(document.ITTermination.EmployeeName.value =="")

{

alert(" Please enter ''员工姓名'的名字和姓氏'

仅使用字符。");

document.ITTermination.EmployeeName.focus();

返回(false);

}

返回有效;

}


/ / - >


< / script>


这是包含在

onBlur事件中的验证检查的文本标签信息:


< label>员工姓名:

< input name =" EmployeeName"类型= QUOT;文本" id =" EmployeeName"

style =" background-color:#C0C0C0"大小= QUOT 35 QUOT; maxlength =" 35"

onBlur =" validate_form();" />

< / label>

所以当我大胆了,我决定检查没有数字输入

到现场,并提出这个代码无效:


< script language =" javascript">

<! -

function validate_form(){

valid = true;

if(document.ITTermination.EmployeeName.value =="")

{

alert("请输入''员工的名字和姓氏姓名''使用

只有字符。");

document.ITTermination.EmployeeName.focus();

return(false);

}


else if(document.ITTermination.EmployeeName.value ==

" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw xyz")

{

alert("请不要在员工姓名字段中使用数字。仅

字母字符允许。)

document.ITTermination.EmployeeName.focus();

返回(false);

}

返回有效;

}

// - >

< / script>

我甚至尝试过使用else if(document.ITTermination.EmployeeName.value

==" alpha")并且也没用。


如果有人会非常友好地帮助我,我将前往

下一场。我想继续在这个表格中添加验证

一个脚本validate_form()。


提前谢谢,

Justine

Hello,

I am fairly new to asp and jscript and am now in the process of
learning some form validation. I am taking one step at a time by
validating each field and testing it before moving onto the next one to
be sure I am correct. I ran into a problem with my validation when I
added an else if code to my code.

Here is what I tried to do: Form (ITTermination) has a field
(EmployeeName) which I would like to validate to check for no value and
alpha characters A - Z only, no numbers.

This code worked great to ONLY to validate a no value in the field:

<script language="JavaScript">

<!--

function validate_form ( )
{
valid = true;

if ( document.ITTermination.EmployeeName.value == "" )
{
alert("Please enter first and last name for ''Employee Name''
using only characters." );
document.ITTermination.EmployeeName.focus();
return (false);
}
return valid;
}

//-->

</script>

Here is the text label info with the validation check included in the
onBlur event:

<label>Employee Name:
<input name="EmployeeName" type="text" id="EmployeeName"
style="background-color: #C0C0C0" size="35" maxlength="35"
onBlur="validate_form();"/>
</label>
So when I got daring I decided to check that no numbers are entered
into the field and came up with this code which is not working:

<script language="javascript">
<!--
function validate_form () {
valid = true;
if (document.ITTermination.EmployeeName.value == "" )
{
alert("Please enter first and last name for ''Employee Name'' using
only characters." );
document.ITTermination.EmployeeName.focus();
return (false);
}

else if (document.ITTermination.EmployeeName.value ==
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw xyz")
{
alert("Please do not use numbers within the Employee Name field. Only
alphabetic characters are allowed.")
document.ITTermination.EmployeeName.focus();
return (false);
}
return valid;
}
//-->
</script>
I even tried using else if (document.ITTermination.EmployeeName.value
== "alpha") and that did not work either.

If someone would be so kind to help me out I will be on my way to the
next field. I would like to keep adding validations to this form within
the one script validate_form( ).

Thanks in advance,
Justine

推荐答案

你的其他if语句有询问的值是否等于整个

字母串。除非他们输入所有这些

字符,否则情况并非如此。你需要的是一个返回true或false的函数

如果值是一个数字。


该函数被称为isNaN()对于不是数字并且

就像这样....


isNaN(document.ITTermination.EmployeeName.value)


仅当值为全部字母时才会返回true,否则它将返回false,表示它在某处有一个数字。现在这个

可能不是解决它的全部解决方案,因为我不太确定它是如何处理所有特殊字符的,所以你可能也希望找到一个

自定义函数(可能会使用indexOf函数)来提供完整的傻瓜验证验证。


但是,isNaN应该可以帮到你相当一点,朝着正确的方向。

希望这会有所帮助:)

Your else if statement there is asking does the value equal that entire
string of letters. Which is not the case unless they entered all those
characters. What you need is a function that will return true or false
if the value is a number or not.

That function is called isNaN() which stands for "Is Not a Number" and
it works like this....

isNaN(document.ITTermination.EmployeeName.value)

This will return true only if the value is all letters, otherwise it
will return false saying that it has a number in it somewhere. Now this
may not be the solve it all solution because I am not quite sure how it
will handle all special characters, so you might want to also find a
custom made function (which probably will use indexOf function) to
offer complete fool proof validation.

However, isNaN should help you quite a bit and in the right direction.
Hope this helps :)


JN ***** @ gmail.com 写道:
你好,

我对asp很新和jscript我现在正在学习一些表格验证的过程。我一步一步地通过验证每个字段并测试它,然后再转到下一个字段来确保我是正确的。当我在代码中添加了其他代码时,我遇到了验证问题。

这是我尝试做的:Form(ITTermination)有一个字段
(EmployeeName)我想验证以检查没有值和
字母A - Z,没有数字。

此代码非常适用于仅验证无值字段:

< script language =" JavaScript">

< script type =" text / javascript">

<! -

函数validate_form()
{
有效= true;

if(document.ITTermination.EmployeeName.value ==" ;")
{
警告(请输入''员工姓名''的名字和姓氏。
仅使用字符。);
document.ITTermination。 EmployeeName.focus();
return(false);


为什么不是有效=假?使功能更好。

如果(document.ITTermination.EmployeeName.value ==
" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw xyz")
Hello,

I am fairly new to asp and jscript and am now in the process of
learning some form validation. I am taking one step at a time by
validating each field and testing it before moving onto the next one to
be sure I am correct. I ran into a problem with my validation when I
added an else if code to my code.

Here is what I tried to do: Form (ITTermination) has a field
(EmployeeName) which I would like to validate to check for no value and
alpha characters A - Z only, no numbers.

This code worked great to ONLY to validate a no value in the field:

<script language="JavaScript">
<script type="text/javascript">
<!--

function validate_form ( )
{
valid = true;

if ( document.ITTermination.EmployeeName.value == "" )
{
alert("Please enter first and last name for ''Employee Name''
using only characters." );
document.ITTermination.EmployeeName.focus();
return (false);
Why not valid = false? Makes the function flow better.


else if (document.ITTermination.EmployeeName.value ==
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw xyz")




这是一个精确的比较,你想要的是一个正则表达式匹配,

尝试像value.match(\ ^ [a-zA-Z]



This is an exact compare, what you want is a regular expression match,
try something like value.match( \^[a-zA-Z]


\)。


阅读正则表达式,它们是验证

字符串的强大工具。


-

Ian Collins。
\ ).

Read up on regular expressions, they are a powerful tool for validating
strings.

--
Ian Collins.


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

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