任何人都可以帮助我在javascript中对此进行验证吗 [英] Can anyone help me out to validate this in javascript please

查看:78
本文介绍了任何人都可以帮助我在javascript中对此进行验证吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我正在用HTML,PHP和JavaScript构建一个独立的页面.我已经设法完成了我的PHP,但是由于我是新手,所以开始努力启动JavaScript.如果有人可以指导我,那就太好了.谢谢,这是下面的我的HTML代码:

Hi there

I am building a stand alone page in HTML, PHP and JavaScript. I have managed to complete my PHP but strangling to start JavaScript as i am new to it. If someone could guide me that would be awesome. Thanks here is my HTML code below:

<div id="form">
		<form name="registration" action="validation.php" method="POST">
			<h3>Registration Form</h3>
			<fieldset id="Logon">
				<legend>Create Logon</legend>
				
				<!-- add a class to all the input boxes instead. Safer and more backwards-compatible -->
                <div class="form-line">
				<label for="username">Username *: </label>
				<input type="text" id="Username" name="userid"/></div>
				
                <div class="form-line">
				<label for="pwd">Password *: </label>
				<input type="password" id="Password" name="pwd" /></div>
				
				<div class="form-line">
				<label for="pwd">Confirm Password *: </label>
				<input type="password" id="Confirm Password" name="confpwd"/></div>
				
				</fieldset>	
			
			<br />
			
			<fieldset id="Details">
				<legend>User Details</legend>
                <div class="form-line">
				<label for="lastname">Surname *: </label>
				<input type="text" id="Surname" name="lastname"/></div>
				
                <div class="form-line">
				<label for="other_names">Other Names *: </label>
				<input type="text" id="Other Names" name="names" /></div>
				
                <div class="form-line">
				<label for="dob">Date of Birth *: </label>
				<input type="text" id="Date of Birth" name="dob"/></div>
				
                <div class="form-line">
				<label for="email">Email *: </label>
				<input type="email" id="Email Address" name="email"/></div>
				
				<div class="form-line">
				<label for="email">Confirm Email *: </label>
				<input type="email" id="Confirm Email" name="confemail" /></div>
				
                <div class="form-line">
				<label for="tel">Telephone: </label>
				<input type="text" id="Telephone" name="tel" /></div>
				
                <div class="form-line">
				<label for="add">Address line 1 *: </label>
				<input type="text" id="Address Line 1" name="add1"/></div>
				
				<div class="form-line">
				<label for="add">Address line 2: </label>
				<input type="text" id="Address Line 2" name="add2" /></div>
				
				<div class="form-line">
				<label for="add">Town/City *: </label>
				<input type="text" id="Town or City" name="twnc"/></div>
				
                <div class="form-line">
				<label for="ptc">Post Code *: </label>
				<input type="text" id="Post Code" name="ptc" /></div>
				
                <input type="submit" value="Submit" />
				<input type="reset" value="Reset" />
 
                <p align="center">Note: All fields marked with * must be completed!.</p>
            </fieldset>	
			</form>
        </div>
		</div> 
		<br />
	        <div id="footer"></div> 
			<p align="center"; font "">Copyright © Your Online Library. All rights reserved </p>
    </body>
</html>



我想即时验证用户输入,例如用户名,以检查用户名是否已存在于数据库中. PHP正在服务器端进行验证,现在我要一个Java类来验证最终用户输入.我听说使用ajax可以实时进行验证,但是我以前从未使用过ajax.我不介意它是否是ajax,当用户在注册表单上输入信息时,我只是想能够验证以下内容:

用户名,密码,姓氏,其他名称,DOB,电话,电子邮件,地址行1,地址行2,城镇/城市和邮政编码.

遵循下面给出的第一个解决方案,但有错误:(
在此先感谢



I want to validate the user inputs such as username on the fly to check if username already exists in the database. The php is validating on the server side, now i want a java class to validate end user inputs. I heard using ajax does validation on the fly, but i have never used ajax before. I m not bothered if it is ajax or not i just want to be able to validate the following when user enters information on the registration form:

Username, Password, Surname, Other names, DOB, Tel, Email, Address line 1, address line 2, Town/city and post code.

Something along the lines of the first solution given below, but it has errors :(
Thanks in advance

推荐答案

尝试一下,

< input type ="submit" value ="Submit" onclick =返回validate();" />

Try this,

<input type="submit" value="Submit" onclick="return validate();" />

<script type="text/javascript">
function validate()
{
var name=document.getElementById("Username");
if(name.value=="")
{
alert("Please enter username");
name.focus();
return false;
}
var Password=document.getElementById("Password");
if(Password.value=="")
{
alert("Please enter Password");
Password.focus();
return false;
}
var rePassword=document.getElementById("Confirm Password");
if(rePassword.value=="")
{
alert("Please re-enter Password");
rePassword.focus();
return false;
}
var dob=document.getElementById("Date of Birth");
if(dob.value=="")
{
alert("Please re-enter Password");
dob.focus();
return false;
}
var email=document.getElementById("Email Address");
if(email.value=="")
{
alert("Please re-enter Password");
email.focus();
return false;
}
var cemail=document.getElementById("Confirm Email");
if(cemail.value=="")
{
alert("Please re-enter Password");
cemail.focus();
return false;
}
var address=document.getElementById("Address Line 1");
if(address.value=="")
{
alert("Please re-enter Password");
address.focus();
return false;
}
var city=document.getElementById("Town or City");
if(city.value=="")
{
alert("Please re-enter Password");
city.focus();
return false;
}
var pin=document.getElementById("Post Code");
if(pin.value=="")
{
alert("Please re-enter Password");
pin.focus();
return false;
}
}
</script>




这是简单的验证.如果您需要详细的验证方法,则需要详细检查,例如电子邮件格式,仅字母等,

谢谢.




This is the simple validation. If you want detail validation means, you need to check detailly, like email format,only alphabets etc.,

Thanks.


这篇关于任何人都可以帮助我在javascript中对此进行验证吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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