编写Java代码进行验证的位置 [英] where to write Javascript code for validation

查看:66
本文介绍了编写Java代码进行验证的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
我有两个名为validatePhone()validateEmail()的Javascript函数,以及两个名为txtPhonetxtEmail的文本框,以及一个用于放置脚本以及如何调用该脚本的按钮submit

谢谢你

hi
I have two Javascript functions named validatePhone() and validateEmail() and two text boxes named txtPhone and txtEmail and a button submit where to place my script and how to call that

thank you

推荐答案

我怀疑你的意思是Javascript.
您是否不认为Javascript与Java没有关系?这两种语言都不是由另一种语言衍生而来,甚至没有受到影响.
Java不用于脚本编写,它完全基于编译为字节码.


在所有这些否决之后,我可以看到有人直到我重复一遍才明白:这个问题询问"java脚本".哪有这回事!如果问题是关于Javascript的,我想在问题的标题和正文中准确看到此名称.
如果是Java,我将解释为与Java无关.


如果还不够,请观看: http://www.youtube.com/watch?v=u9_kahA_wQo [ ^ ].

现在好些了吗?


现在,有关脚本语言的误解可能已经消除:关于脚本的位置.

有两个位置:外部文件中的脚本(将其放置在"scripts/mylib.js"中)和嵌入HTML中.让我在下面显示两种情况:

I suspect you mean Javascript.
Do you no that Javascript has nothing to do with Java? None of those two languages was derived by another one or even influenced.

Java is not used for scripting, it''s totally based on compilation to byte code.


After all those down-votes, I can see someone won''t understand until I repeat: the question asks about "java script". There is no such thing! If the question was about Javascript, I want to see exactly this name in the title and body of the question.
If it is Java, I explain that it is something that has nothing to do with Javascript.


If this is not enough, watch this: http://www.youtube.com/watch?v=u9_kahA_wQo[^].

Better now?


Now when the misunderstanding about the scripting language is probably cleared: about position of the script.

There are two places: a script in external file (let''s place it in "scripts/mylib.js") and embedded in HTML. Let me show both cases below:

<html>
   <head>
      <title>My title</title>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <script type="text/javascript" src="./scripts/mylib.js" />
      <script type="text/javascript"><!--
         //Javascript functions go here
      --></script>
   </head>
<body>

<!-- page content here -->

</body>
</html>



-SA



—SA


< script language =:javascript& quot; type =& quot;文本/javascript& quot;& gt;
函数validate()
{
if(document.getElementById(& lt;%= txt_id.ClientID%& gt;& quot;).value ==& quot;& quot;)
{
alert(&用户ID不能为空& quot;));
document.getElementById(&& lt;%= txt_id.ClientID%& gt;& quot;).focus();
返回false;
}
if(document.getElementById(& lt;%= txtfname.ClientID%& gt;& quot;).value ==& quot;& quot;)
{
alert("名姓不能为空");
document.getElementById(&& lt;%= txtfname.ClientID%& gt;&").focus();
返回false;
}
if(document.getElementById(& lt;%= txtlname.ClientID%& gt;& quot;).value ==& quot;& quot;){
警报("姓氏不能为空");
document.getElementById(&& lt;%= txtlname.ClientID%& gt;&").focus();
返回false;
}
if(document.getElementById(& lt;%= txtemail.ClientID%& gt;& quot)).value ==& quot;& quot;)
{
警报(电子邮件地址不能为空")
document.getElementById(&& lt;%= txtemail.ClientID%& gt;& quot)).focus();
返回false;
}
var emailPat =/^(\&".*\&"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3 }){3}] || [A-Za-z] \ w *(\.[A-Za-z] \ w *)+)
<script language=:javascript&quot; type=&quot;text/javascript&quot;&gt;
function validate()
{
if (document.getElementById(&quot;&lt;%=txt_id.ClientID%&gt;&quot;).value == &quot;&quot;)
{
alert(&quot;User Id Feild can not be blank&quot;);
document.getElementById(&quot;&lt;%=txt_id.ClientID%&gt;&quot;).focus();
return false;
}
if (document.getElementById(&quot;&lt;%=txtfname.ClientID%&gt;&quot;).value == &quot;&quot;)
{
alert(&quot;First Name Feild can not be blank&quot;);
document.getElementById(&quot;&lt;%=txtfname.ClientID%&gt;&quot;).focus();
return false;
}
if (document.getElementById(&quot;&lt;%=txtlname.ClientID%&gt;&quot;).value == &quot;&quot;) {
alert(&quot;Last Name Feild can not be blank&quot;);
document.getElementById(&quot;&lt;%=txtlname.ClientID%&gt;&quot;).focus();
return false;
}
if (document.getElementById(&quot;&lt;%=txtemail.ClientID %&gt;&quot;).value == &quot;&quot;)
{
alert(&quot;Email address can not be blank&quot;);
document.getElementById(&quot;&lt;%=txtemail.ClientID %&gt;&quot;).focus();
return false;
}
var emailPat = /^(\&quot;.*\&quot;|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)


/;
var emailid = document.getElementById(&& lt;%= txtemail.ClientID%& gt;& quot;).value;
var matchArray = emailid.match(emailPat);
if(matchArray == null){
警报(&您的电子邮件地址似乎不正确.请重试.&");
document.getElementById(&& lt;%= txtemail.ClientID%& gt;& quot)).focus();
返回false;
}
if(document.getElementById(& lt;%= txtuname.ClientID%& gt;& quot;).value ==& quot;& quot;))
{
alert(&用户名不能为空&");
document.getElementById(&& lt;%= txtuname.ClientID%& gt;&").focus();
返回false;
}
if(document.getElementById(& lt;%= txtpass.ClientID%& gt;& quot;).value ==& quot;& quot;))
{
alert(& quot;密码不能为空& quot;);
document.getElementById(&& lt;%= txtpass.ClientID%& gt;&").focus();
返回false;
}
var digits =& quot; 0123456789& quot ;;
var temp;
for(var i = 0; i& lt; document.getElementById(& lt;%= txt_id.ClientID%& gt;& quot;).value.length; i ++)
{
temp = document.getElementById(&& lt;%= txt_id.ClientID%& gt;&").value.substring(i,i + 1);
如果(digits.indexOf(temp)== -1)
{
警报(&请输入有效的ID& quot;);
document.getElementById(&& lt;%= txt_id.ClientID%& gt;& quot;).focus();
返回false;
}
}
var digits =& quot; abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ& quot;
var temp;
for(var i = 0; i& lt; document.getElementById(& lt;%= txtfname.ClientID%& gt;& quot;).value.length; i ++){
temp = document.getElementById(&& lt;%= txtfname.ClientID%& gt;&").value.substring(i,i + 1);
如果(digits.indexOf(temp)== -1){
警报(&请仅输入字符...&");
document.getElementById(&& lt;%= txtfname.ClientID%& gt;&").focus();
返回false;
}
}
var digits =& quot; abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ& quot;
var temp;
for(var i = 0; i& lt; document.getElementById(& lt;%= txtlname.ClientID%& gt;& quot;).value.length; i ++){
temp = document.getElementById(&& lt;%= txtlname.ClientID%& gt;&").value.substring(i,i + 1);
如果(digits.indexOf(temp)== -1){
警报(&请仅输入字符...&");
document.getElementById(&& lt;%= txtlname.ClientID%& gt;&").focus();
返回false;
}
}
返回true;
}
</script>
/;
var emailid = document.getElementById(&quot;&lt;%=txtemail.ClientID %&gt;&quot;).value;
var matchArray = emailid.match(emailPat);
if (matchArray == null) {
alert(&quot;Your email address seems incorrect. Please try again.&quot;);
document.getElementById(&quot;&lt;%=txtemail.ClientID %&gt;&quot;).focus();
return false;
}
if (document.getElementById(&quot;&lt;%=txtuname.ClientID%&gt;&quot;).value == &quot;&quot;)
{
alert(&quot;Username can not be blank&quot;);
document.getElementById(&quot;&lt;%=txtuname.ClientID%&gt;&quot;).focus();
return false;
}
if (document.getElementById(&quot;&lt;%=txtpass.ClientID%&gt;&quot;).value == &quot;&quot;)
{
alert(&quot;Password can not be blank&quot;);
document.getElementById(&quot;&lt;%=txtpass.ClientID%&gt;&quot;).focus();
return false;
}
var digits = &quot;0123456789&quot;;
var temp;
for (var i = 0; i &lt; document.getElementById(&quot;&lt;%=txt_id.ClientID%&gt;&quot;).value.length; i++)
{
temp = document.getElementById(&quot;&lt;%=txt_id.ClientID%&gt;&quot;).value.substring(i, i + 1);
if (digits.indexOf(temp) == -1)
{
alert(&quot;Please enter valid Id&quot;);
document.getElementById(&quot;&lt;%=txt_id.ClientID%&gt;&quot;).focus();
return false;
}
}
var digits = &quot;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ &quot;;
var temp;
for (var i = 0; i &lt; document.getElementById(&quot;&lt;%=txtfname.ClientID%&gt;&quot;).value.length; i++) {
temp = document.getElementById(&quot;&lt;%=txtfname.ClientID%&gt;&quot;).value.substring(i, i + 1);
if (digits.indexOf(temp) == -1) {
alert(&quot;Please enter character only...&quot;);
document.getElementById(&quot;&lt;%=txtfname.ClientID%&gt;&quot;).focus();
return false;
}
}
var digits = &quot;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ &quot;;
var temp;
for (var i = 0; i &lt; document.getElementById(&quot;&lt;%=txtlname.ClientID%&gt;&quot;).value.length; i++) {
temp = document.getElementById(&quot;&lt;%=txtlname.ClientID%&gt;&quot;).value.substring(i, i + 1);
if (digits.indexOf(temp) == -1) {
alert(&quot;Please enter character only...&quot;);
document.getElementById(&quot;&lt;%=txtlname.ClientID%&gt;&quot;).focus();
return false;
}
}
return true;
}
</script>


这篇关于编写Java代码进行验证的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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