Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #20500 > unrolled thread

Javascript validation error

Started byManju Bishnoi <manjubishnoi09@gmail.com>
First post2013-06-30 22:13 -0700
Last post2013-07-01 09:11 +0200
Articles 2 — 2 participants

Back to article view | Back to comp.lang.javascript


Contents

  Javascript validation error Manju Bishnoi <manjubishnoi09@gmail.com> - 2013-06-30 22:13 -0700
    Re: Javascript validation error Stefan Weiss <krewecherl@gmail.com> - 2013-07-01 09:11 +0200

#20500 — Javascript validation error

FromManju Bishnoi <manjubishnoi09@gmail.com>
Date2013-06-30 22:13 -0700
SubjectJavascript validation error
Message-ID<1f9d2bbf-3c91-4d0d-a096-11eed7897a79@googlegroups.com>
Javascript validation field by field


<%@ page import="da.ma.volks.Connector" %>
<%@page import="java.sql.*"%>
<%@ page import="java.awt.*" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<script language="javascript" src="mail.js"></script> 

<script type="text/javascript">

function verify()
{
	

		var letters = /^[A-Za-z]+$/;
for( var i=0;i<document.getElementById("Registration").value.length;i++)
	{
		if (document.Registration.fname.value == ""
				|| document.Registration.fname.value == null) {
			alert(" firstname cannot be blank");
			document.Registration.fname.focus();
			
			return false;
		}
		else
		if (!document.Registration.fname.value.match(letters)) {
			alert(" Enter alphabetics only in fname");
			document.Registration.fname.focus();
			return false;
		}
		else
		if (document.Registration.lname.value == ""
				|| document.Registration.lname.value == null) {
			alert("LastName cannot be blank ");
			document.Registration.lname.focus();
			return verifyfname();
		}

		if (!document.Registration.lname.value.match(letters)) {
			alert(" Enter alphabetics only");
			document.Registration.lname.focus();
			return false;
		}

		if (document.Registration.uid.value == "") {
			alert("userid cannot be blank");
			document.Registration.uid.focus();
			return false;
		}
		var letter = "_";
		if (document.Registration.uid.value.match(letter)) {
			alert("underscores not allowed");
			document.Registration.uid.focus();
			return false;
		}

		if (document.Registration.pwd.value == "") {
			alert("password cannot be blank");
			document.Registration.pwd.focus();
			return false;
		}

		if (document.Registration.pwd.value.length < 5) {
			alert("Password must be more than 5 characters");
			document.Registration.pwd.focus();
			return false;
		}

		if (document.Registration.cfmpwd.value == "") {
			alert("confirmpassword cannot be blank");
			document.Registration.cfmpwd.focus();
			return false;
		}
		if (document.Registration.cfmpwd.value.length < 5) {
			alert(" confirmpassword should be more  than 5 characters");
			document.Registration.cfmpwd.focus();
			return false;
		}

		if ((document.Registration.pwd.value) != (document.Registration.cfmpwd.value)) {
			alert("Your password does not match");
			document.Registration.cfmpwd.value == "";
			document.Registration.cfmpwd.focus();
			return false;
		}

		if (document.Registration.country.value == "") {
			alert(" country cannot be blank");
			document.Registration.country.focus();
			return false;
		}

		if (document.Registration.mobile.value == "") {
			alert("Mobilenumber cannot be blank");
			document.Registration.mobile.focus();
			return false;
		}

		var d = new Date();
		var date = d.getDate();
		var month = d.getMonth() + 1;
		var year = d.getFullYear();

		var cdate = document.Registration.birthday.value;
		var cmonth = document.Registration.birthday_month.value;
		var cyear = document.Registration.birthday_year.value;
		if (document.Registration.birthday_month.value == "") {
			alert("Mobilenumber cannot be blank");
			document.Registration.birthday_month.focus();
			return false;
		}

		if (cmonth > month && cyear > year) {
			alert("Month is not valid month");
			document.Registration.birthday_month.focus();
			return false;
		}
		if (cyear > year) {
			alert("Year is not  valid year");
			document.Registration.birthday_year.focus();
			return false;
		}
		if (cdate > date && cmonth > month && cyear > year) {
			alert(" Date is not valid date");
			document.Registration.birthday.focus();
			return false;
		}
	}
		return true;
	}

	function getCountryCode() {
		var xmlhttp;
		if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp = new XMLHttpRequest();
		} else {// code for IE6, IE5
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				//alert(CountryCode);
				document.getElementById("CountryCode").value = xmlhttp.responseText;
			}
		}
		var country = document.getElementById("country").value;
		xmlhttp.open("GET", "getCountryCode.jsp?country=" + country, true);
		xmlhttp.send();
	}
</script>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registration Page</title>

</head>
<body bgcolor="#33CCFF" onload="document.Registration.fname.focus()">

<center>
 <font face="Times New Roman,Times" size="+3">
 Registration Page</font>
 </center>
            
   <div align="center">
      <form name="Registration"  action="Process.jsp" method="POST"  >
                <input type="hidden" name="expid" value="%% EXPID %%"  >
                <div></div>
              <center><table cellspacing="2" cellpadding="4" border="0"  >
                  <tbody >
                   <tr>
                      <td class="normalfont"><font face="calibri" size="+1">FirstName</font> </td>
                      <td>
                        <input type="text" class="formInput" name="fname" onblur="return verify(this.value,1,0)"  /></td>
                        
                    </tr>
                    <tr>
                      <td class="normalfont"><font face="calibri" size="+1">LastName:</font></td>
                      <td>
                        <input type="text" class="formInput" name="lname"  onblur="return verify(this.value,1,0)" /></td>
                    </tr>
                    <tr>
                      <td ><font face="calibri" size="+1">Preferred UserId</font></td>
                      <td>
                        <input type="text" class="formInput" name="uid" onblur="return verify(this.value,1,0)" /></td>
                    </tr>
                    <tr>
                      <td ><font face="calibri" size="+1">Password</font></td>
                      <td>
                        <input type="password" class="formInput" name="pwd" onblur="return verify(this.value,1,0)" /></td>
                    </tr>
                    <tr>
                      <td class="normalfont"><font face="calibri" size="+1">Confirm Password</font></td>
                      <td>
                      <input type="password" class="formInput" name="cfmpwd" onblur="return verify(this.value,1,0)" /></td>
                    </tr>
                   
                     <tr>
                      <td class="normalfont"><font face="calibri" size="+1">Country</font></td>
                      <td>
                      <select name="country" id="country" onchange="getCountryCode()" >
                      	<option>- - Select Country - - </option>
                      	<%
                      		Connection conn =Connector.getCon();
                      		PreparedStatement psmt = conn.prepareStatement("select COUNTRY from ISDCODE where status='Active'");
                      		ResultSet rs = psmt.executeQuery();
                      		while(rs.next())
                      		{
                      			String country = rs.getString(1);
                      			%>
                      			<option value="<%=country%>"><%=country %></option>
                      			 
                      		<%}
                      	
                      	%>
                      </select>
                    </tr>
                    <tr>
                      <td class="normalfont"><font face="calibri" size="+1">Email</font></td>
                      <td>
                        <input type="text" class="formInput" name="email" onblur="return (ValidateForm());" /></td>
                    </tr>
                    <tr>

Here is my code, i am getting continuous alert messages without typing any data in the field. How can i resolve this issue. I am new to Java Enviornment. Please suggest me.

[toc] | [next] | [standalone]


#20501

FromStefan Weiss <krewecherl@gmail.com>
Date2013-07-01 09:11 +0200
Message-ID<kqra2n$3o3$1@news.albasani.net>
In reply to#20500
On 2013-07-01 07:13, Manju Bishnoi wrote:
> <%@ page import="da.ma.volks.Connector" %>
> <%@page import="java.sql.*"%>
> <%@ page import="java.awt.*" %>
> <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
>     pageEncoding="ISO-8859-1"%>

This group is about JavaScript and related languages, but not about
Java. Java and JavaScript are two distinct and very different languages.
If you need assistance with JS, don't show us the JSP source. Show us
the resulting HTML that the browser receives, instead.

> 		var letters = /^[A-Za-z]+$/;

Both first name and last name must match this. How would a person named
"José Vianna da Motta" register?

> for( var i=0;i<document.getElementById("Registration").value.length;i++)

This loop is pointless:
- You never use the i variable after this.
- document.getElementById("Registration") gets you the <form> element,
  which doesn't have a "value" property. That's the first error you get.
- Even if this would loop over all the elements, the function already
  contains checks for each input field, so why the additional loop?

> 		if (document.Registration.fname.value == "

That's not a good way to reference form elements. Better:
   var elements = document.forms.Registration.elements;
and use elements.fname, elements.lname, etc after that.

   [snip many lines...]

> Here is my code, i am getting continuous alert messages without
> typing any data in the field. How can i resolve this issue. I am new to
> Java Enviornment. Please suggest me.

Every time the user enters a value and focuses the next field, ALL the
fields are checked, which will inevitably lead to an annoying JS alert.
My advice would be to let the users enter whatever they want, without
validation. Only when the user submits the form should your script check
the values.

You should also invest some time into reading up on the difference
between Java and JavaScript, and on current best practices for HTML
(i.e., don't start a new project using HTML 4.01 Transitional in 2013).


- stefan

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web