电话验证问题(我重新格式化了代码以便于阅读) [英] Phone Validation Problem (I reformatted the code to make it easier to read)

查看:67
本文介绍了电话验证问题(我重新格式化了代码以便于阅读)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<! -

函数验证(theForm)

{

var validity = true; //假设有效

if(frmComments.name.value ==''''&& validity == true)

{

alert(''你的全名是必需的。请输入你的全名!'');

validity = false;

frmComments.name.focus();

event.returnValue = false;

}

if(frmComments.company.value ==''''&& validity ==真的)

{

alert(''您的公司名称是必需的。请输入您的

公司的名称!'');

validity = false;

frmComments.company.focus();

event.returnValue = false;

}

var pattern1 = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\。 \w {2,4})+ $ /;

if(!pattern1.test(frmComments.email.value)&& validity == true)

{

alert(无效的电子邮件地址!请重新输入。)

frmComments.email.focus();

validity = false;

r eturn false;

}

if(frmComments.zip.value =="" )

返回true;

else

{

var pattern2 = / \d {5} /;

if(pattern2.test(frmComments.zip.value))

返回true;

else

{

if(!pattern2.test(frmComments.zip.value)&& validity == true)

{

alert(" ;邮政编码无效!必须是12345表格。请重新输入。);

frmComments.zip.focus();

validity = false;

返回false;

}

}

}

if(frmComments.phone。值=="")

返回true;

else

{

var pattern3 = / \\ \\ n {3} \-\d {3} \-\d {4} /;

if(pattern3.test(frmComments.phone.value))

返回true;

else

{

if(!pattern3.test(frmComments.phone.value)&&有效期== true)

{

提醒(无效的电话号码!必须是表格555-555-5555。请

re - 输入。&q uot;);

frmComments.phone.focus();

validity = false;

返回false;

}

}

}

}


函数formSubmit()

{

window.event.returnValue = false;

if(确认(你确定要提交吗?) ))

window.event.returnValue = true;

}

函数formReset()

{

window.event.returnValue = false;

if(确认(你确定要重置吗?))

{

frmComments.name.focus();

window.event.returnValue = true;

}

}

// - >

< / script>


action =" http://matrix.csis.pace.edu/~f03-it604-s03/cgi-bin/comments.pl"

method =" post" onSubmit =" return validate(this);" onReset =" return

formReset(this)">


< input type =" submit"对齐= QUOT;右"值= [提交" name =" submit"

onclick =" formSubmit()">


------------- ----------------------------------------------

这是html页面的链接:

http://matrix.csis.pace.edu/~f03-it6.../comments.html


我尝试通过手动格式化代码使代码更容易阅读

删除缩进和换行符。我不知道你建议用一定数量的列格式化你的技术。请

让我知道怎么做。


我是这个小组的新手,可能会做错事,直到学习。

我不知道你是否还记得我的问题:


我的手机验证在我的验证功能中无效。单独运行

,但不像其他验证那样在这个函数内。


手机不是必需的,但如果用户输入了一个电话号码,它必须

的格式为555-555-5555。


此外,我的提交确认对话框首先出现,

不断。我想让它在最终有效的

提交之前出现在最后一次。


请告诉我一个正确的,更好的方法来验证这些

必填字段:姓名,公司,电子邮件和验证电话

如果用户填写电话号码,格式正确。


感谢您的帮助............................. == true)

{

alert(无效的电子邮件地址!请重新输入。)

frmComments.email .focus();

validity = false;

返回false;

}

if(frmComments.zip .value =="")

返回true;

else

{

var pattern2 = / \d {5} /;

if(pattern2.test(frmComments.zip.value))

返回true;

else

{

if(!pattern2.test(frmComments.zip.value)&& validity == true)
{

alert("邮政编码无效!)必须是12345表格。请重新输入。);

frmComments.zip.focus();

validity = false;

返回false;

}

}

}

if(frmComments.phone.value ==" ")

返回true;

else

{

var pattern3 = / \d {3} \-\d {3} \-\d {4} /;

if(pattern3.test(frmComments.phone.value))

return true;

else

{

if(!pattern3.test(frmComments.phone.value)&& validity == true)

{

alert(无效的电话号码!必须在表格555-555-5555。请

重新输入。 );

frmComments.phone.focus();

validity = false;

返回false;

}

}

}

}


函数formSubmit()

{

window.event.returnValue = false;

if(确认(你确定要提交吗?))

window.event.returnValue = true;

}

函数formReset()

{

window.event。 returnValue = false;

if(确认(你确定要重置吗?) ))

{

frmComments.name.focus();

window.event.returnValue = true;

}

}

// - >

< / script>


< form id =" frmComments"

action =" http://matrix.csis.pace.edu/~f03-it604-s03/cgi-bin/comments.pl"

method =" post" onSubmit =" return validate(this);" onReset =" return

formReset(this)">


< input type =" submit"对齐= QUOT;右"值= [提交" name =" submit"

onclick =" formSubmit()">


------------- ----------------------------------------------

这是html页面的链接:

http://matrix.csis.pace.edu/~f03-it6.../comments.html


我尝试通过手动格式化代码使代码更容易阅读

删除缩进和换行符。我不知道你建议用一定数量的列格式化你的技术。请

让我知道怎么做。


我是这个小组的新手,可能会做错事,直到学习。

我不知道你是否还记得我的问题:


我的手机验证在我的验证功能中无效。单独运行

,但不像其他验证那样在这个函数内。


手机不是必需的,但如果用户输入了一个电话号码,它必须

的格式为555-555-5555。


此外,我的提交确认对话框首先出现,

不断。我想让它在最终有效的

提交之前出现在最后一次。


请告诉我一个正确的,更好的方法来验证这些

必填字段:姓名,公司,电子邮件和验证电话

如果用户填写电话号码,格式正确。


感谢您的帮助。


文章< c6 ************************* @ posting.google .COM> ,
ca*****@optonline.net 启发我们...

< script language =" JavaScript" type =" text / javascript">
<! -
函数验证(theForm)
{
var validity = true; //假设有效
if(frmComments.name.value ==''''&& validity == true)
{
警告(''您的全名是必需的。请输入你的全名!'');
validity = false;
frmComments.name.focus();
event.returnValue = false;
}


简单的返回false就足够了。


frmComments.name.focus();

返回false;

}

if(frmComments.zip.value =="")
如果用户根本没有输入任何内容,则某些值中的值未定义

浏览器。因此,您无法将其与空字符串进行比较。我有这种类型

的比较在IE版本中惨遭失败。所以现在,如果(!frmComments.zip || frmComments.zip.value =="")

返回true;
/>其他
{
var pattern2 = / \d {5} /;
if(pattern2.test(frmComments.zip.value))
return true;

{
if(!pattern2.test(frmComments.zip.value)&& validity == true)
{
警告("邮政编码无效) !必须是12345表格。请重新输入。);
frmComments.zip.focus();
validity = false;


如果你马上回来,没有任何意义。

实际上,根本没有任何意义,因为失败后返回false

防止再运行任何代码。

返回false;
}
}
}
if(frmComments.phone.value ==" ;")


再次,测试对象,然后测试值。

你说它不起作用,但你不是解释什么不起作用。

它会抛出错误吗?它是否说它不是有效的?它是否说它

是否有效?

函数formSubmit()
{
window.event.returnValue = false;
if(确认(你确定要提交吗?))
window.event.returnValue = true;


返回true会没事。


if(确认...)返回true

else返回false


< form id =" frmComments"
action =" http://matrix.csis.pace.edu/~f03-it604-s03/cgi- bin / comments.pl"
method =" post" onSubmit =" return validate(this);" onReset =" return
formReset(this)">

< input type =" submit"对齐= QUOT;右"值= [提交" name =" submit"
onclick =" formSubmit()">



onClick在onSubmit之前触发,因此确认会先触发。

这就是为什么会这样。从提交

按钮的onClick中删除它,并将onSubmit更改为...


onSubmit =" if(validate(this))formSubmit() ;"


在你更改了这个东西之后,告诉我们手机验证有什么问题,看看它是否还行不通。


-

~kaeli~

为什么知之甚少的人最了解它?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace


感谢您的帮助。


***通过Developersdex发送 http://www.developersdex.com ***

不要只是参加USENET ......获得奖励!

<script language="JavaScript" type="text/javascript">
<!--
function validate(theForm)
{
var validity = true; // assume valid
if(frmComments.name.value=='''' && validity == true)
{
alert(''Your full name is required. Please enter your full name!'');
validity = false;
frmComments.name.focus();
event.returnValue=false;
}
if(frmComments.company.value=='''' && validity == true)
{
alert(''Your company name is required. Please enter the name of your
company!'');
validity = false;
frmComments.company.focus();
event.returnValue=false;
}
var pattern1 = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/;
if (!pattern1.test(frmComments.email.value) && validity == true)
{
alert("Invalid E-mail Address! Please re-enter.")
frmComments.email.focus();
validity = false;
return false;
}
if(frmComments.zip.value == "" )
return true;
else
{
var pattern2 = /\d{5}/;
if(pattern2.test(frmComments.zip.value))
return true;
else
{
if(!pattern2.test(frmComments.zip.value) && validity == true)
{
alert("Invalid Zip Code! Must be in form 12345. Please re-enter.");
frmComments.zip.focus();
validity = false;
return false;
}
}
}
if(frmComments.phone.value == "")
return true;
else
{
var pattern3 = /\d{3}\-\d{3}\-\d{4}/;
if(pattern3.test(frmComments.phone.value))
return true;
else
{
if(!pattern3.test(frmComments.phone.value)&& validity == true)
{
alert("Invalid Phone Number! Must be in form 555-555-5555. Please
re-enter.");
frmComments.phone.focus();
validity = false;
return false;
}
}
}
}

function formSubmit()
{
window.event.returnValue = false;
if ( confirm ( "Are you sure you want to submit?" ) )
window.event.returnValue = true;
}
function formReset()
{
window.event.returnValue = false;
if ( confirm( "Are you sure you want to reset?" ) )
{
frmComments.name.focus();
window.event.returnValue = true;
}
}
// -->
</script>

<form id="frmComments"
action="http://matrix.csis.pace.edu/~f03-it604-s03/cgi-bin/comments.pl"
method="post" onSubmit="return validate(this);" onReset="return
formReset(this)">

<input type="submit" align="right" value="Submit" name="submit"
onclick="formSubmit()">

-----------------------------------------------------------
This is the link to the html page:

http://matrix.csis.pace.edu/~f03-it6.../comments.html

I tried to make the code easier to read by manually formatting it by
removeing the indents and line breaks. I don''t know the technique you
suggested of formatting it with a certain number of columns. Please
let me know how to do that.

I am new to this group and may do things incorrectly until learn.

I don''t know if you remember my problem of:

My phone validation doesn''t work within my validate function. It works
alone, but not within this function like the other validations do.

Phone is not required, but if the user enters a phone number it must
be in the form of 555-555-5555.

Also, my submit confirmation dialog box comes up first and
continuously. I want it to appear last and once before final valid
submission.

Please let me know of a correct and better way of validating these
required fields: name, company, email, and validating phone for
correct format if user fills in phone number.

Thanks for your help.

解决方案

/;
if (!pattern1.test(frmComments.email.value) && validity == true)
{
alert("Invalid E-mail Address! Please re-enter.")
frmComments.email.focus();
validity = false;
return false;
}
if(frmComments.zip.value == "" )
return true;
else
{
var pattern2 = /\d{5}/;
if(pattern2.test(frmComments.zip.value))
return true;
else
{
if(!pattern2.test(frmComments.zip.value) && validity == true)
{
alert("Invalid Zip Code! Must be in form 12345. Please re-enter.");
frmComments.zip.focus();
validity = false;
return false;
}
}
}
if(frmComments.phone.value == "")
return true;
else
{
var pattern3 = /\d{3}\-\d{3}\-\d{4}/;
if(pattern3.test(frmComments.phone.value))
return true;
else
{
if(!pattern3.test(frmComments.phone.value)&& validity == true)
{
alert("Invalid Phone Number! Must be in form 555-555-5555. Please
re-enter.");
frmComments.phone.focus();
validity = false;
return false;
}
}
}
}

function formSubmit()
{
window.event.returnValue = false;
if ( confirm ( "Are you sure you want to submit?" ) )
window.event.returnValue = true;
}
function formReset()
{
window.event.returnValue = false;
if ( confirm( "Are you sure you want to reset?" ) )
{
frmComments.name.focus();
window.event.returnValue = true;
}
}
// -->
</script>

<form id="frmComments"
action="http://matrix.csis.pace.edu/~f03-it604-s03/cgi-bin/comments.pl"
method="post" onSubmit="return validate(this);" onReset="return
formReset(this)">

<input type="submit" align="right" value="Submit" name="submit"
onclick="formSubmit()">

-----------------------------------------------------------
This is the link to the html page:

http://matrix.csis.pace.edu/~f03-it6.../comments.html

I tried to make the code easier to read by manually formatting it by
removeing the indents and line breaks. I don''t know the technique you
suggested of formatting it with a certain number of columns. Please
let me know how to do that.

I am new to this group and may do things incorrectly until learn.

I don''t know if you remember my problem of:

My phone validation doesn''t work within my validate function. It works
alone, but not within this function like the other validations do.

Phone is not required, but if the user enters a phone number it must
be in the form of 555-555-5555.

Also, my submit confirmation dialog box comes up first and
continuously. I want it to appear last and once before final valid
submission.

Please let me know of a correct and better way of validating these
required fields: name, company, email, and validating phone for
correct format if user fills in phone number.

Thanks for your help.


In article <c6*************************@posting.google.com> ,
ca*****@optonline.net enlightened us with...

<script language="JavaScript" type="text/javascript">
<!--
function validate(theForm)
{
var validity = true; // assume valid
if(frmComments.name.value=='''' && validity == true)
{
alert(''Your full name is required. Please enter your full name!'');
validity = false;
frmComments.name.focus();
event.returnValue=false;
}
A simple return false would suffice.

frmComments.name.focus();
return false;
}
if(frmComments.zip.value == "" ) If the user didn''t enter anything at all, value is undefined in some
browsers. Thus, you can''t compare it to the null string. I had this type
of comparison fail miserably in a version of IE. So now, I do

if (!frmComments.zip || frmComments.zip.value == "")
return true;
else
{
var pattern2 = /\d{5}/;
if(pattern2.test(frmComments.zip.value))
return true;
else
{
if(!pattern2.test(frmComments.zip.value) && validity == true)
{
alert("Invalid Zip Code! Must be in form 12345. Please re-enter.");
frmComments.zip.focus();
validity = false;
No point in setting this if you return right after.
Actually, no point in it at all, since returning false after failure
prevents any more code from running.
return false;
}
}
}
if(frmComments.phone.value == "")
Again, test for the object, then the value.
You say it doesn''t work, but you don''t explain WHAT doesn''t work. Does
it throw an error? Does it say it isn''t valid when it is? Does it say it
is valid when it isn''t?

function formSubmit()
{
window.event.returnValue = false; if ( confirm ( "Are you sure you want to submit?" ) )
window.event.returnValue = true;
return true would be fine.

if (confirm...) return true
else return false


<form id="frmComments"
action="http://matrix.csis.pace.edu/~f03-it604-s03/cgi-bin/comments.pl"
method="post" onSubmit="return validate(this);" onReset="return
formReset(this)">

<input type="submit" align="right" value="Submit" name="submit"
onclick="formSubmit()">



onClick fires BEFORE the onSubmit, so the confirm would fire first.
That''s why that is happening. Remove it from the onClick of the submit
button and change onSubmit to...

onSubmit="if (validate(this)) formSubmit();"

Tell more about what''s wrong with the phone validation after you change
this stuff and see if it still doesn''t work.

--
~kaeli~
Why do people who know the least know it the loudest?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace


Thanks for your help.

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!


这篇关于电话验证问题(我重新格式化了代码以便于阅读)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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