Hi all

the problem lies with the createJSON function for some reason it is breaking no idea why though. please advise...

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="style.css" rel="stylesheet" type="text/css">
<title>User Details</title>
<script type="text/javascript">

function validateForm(){
  
  var x=document.forms["myForm"]["username"].value
if (x==null || x=="")
  {
  alert("username must be filled out");
  return false;
  }
  
  var y=document.forms["myForm"]["email"].value
if (y==null || y=="")
  {
  alert("email must be filled out");
  return false;
  }
  
  var z=document.forms["myForm"]["firstname"].value
if (z==null || z=="")
  {
  alert("firstname must be filled out");
  return false;
  }
  
  var a=document.forms["myForm"]["surname"].value
if (a==null || a=="")
  {
  alert("surname must be filled out");
  return false;
  }
  
  var b=document.forms["myForm"]["birthday"].value
if (b==null || b=="")
  {
  alert("birthday must be filled out");
  return false;
  }
  
  var c=document.forms["myForm"]["password"].value
if (c==null || c=="")
  {
  alert("password must be filled out");
  return false;
  }
  
  var d=document.forms["myForm"]["pwdconfirm"].value
if (d==null || d=="")
  {
  alert("pwdconfirm must be filled out");
  return false;
  }
  
  var e=document.forms["myForm"]["gender"].value
if (e=="nothing")
  {
  alert("gender must be chosen");
  return false;
  }
validateEmail();
}

function validateEmail(){
	var h=document.forms["myForm"]["email"].value
	var atpos=h.indexOf("@");
	var dotpos=h.lastIndexOf(".");
	if (atpos<1 || dotpos<atpos+2 || dotpos+2>=h.length){
		alert("Not a valid e-mail address");
		return false;
	}
validateDate();
 }
 
 function validateDate(){
 var chkdate = document.forms["myForm"]["birthday"].value
	if(chkdate.match(/^[0-9]{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])/)){
		createJSON();
	}
	else{
		alert("date format is wrong");
		return false;
	}
 }
 
 function createJSON(){
	
	var User = {
		"username" : document.forms["myForm"]["username"].value,
		"email" : document.forms["myForm"]["email"].value,
		"password" : document.forms["myForm"]["password"].value,
		"firstname" : document.forms["myForm"]["firstname"].value,
		"surname" : document.forms["myForm"]["surname"].value,
		"category" : document.forms["myForm"]["category"].value,
		"gender" : document.forms["myForm"]["gender"].value,
		"birthday" : document.forms["myForm"]["birthday"].value
	};
		alert(User.firstname);
		//var frm1 = document.getElementById("myForm");
		//frm1.action = "display.php";
		//frm1.submit();
		//var jsonstr=JSON.stringify(jsonobj);
}

</script>
</head>
<body>
<div class="center">
<h1>Enter your details user!!</h1>
<form name = "myForm" id = "myForm" novalidate="novalidate">
<table border = "1">
<tr>
	<td>Username: </td>
	<td><input type="text" name="username" /></td>
</tr>
<tr>
	<td>Email: </td>
	<td><input type="email" name="email" /></td>
</tr>
<tr>
	<td>First name: </td>
	<td><input type="text" name="firstname" /></td>
</tr>
<tr>
	<td>Surname: </td>
	<td><input type="text" name="surname" /></td>
</tr>
<tr>
	<td>Date of Birth(YYYY-MM-DD): </td>
	<td><input type="date" name="birthday" /></td>
</tr>
<tr>
	<td>Password: </td>
	<td><input type="password" name="password" /></td>
</tr>
<tr>
	<td>Password Confirm: </td>
	<td><input type="password" name="pwdconfirm" /></td>
</tr>
<tr>
	<td>Gender: </td>
	<td><select name="gender">
			<option value="nothing"></option>
			<option value="Male">M</option>
			<option value="Female">F</option>
		</select>
	</td>
</tr>
<tr>
	<td>Press to Continue:</td>
	<td><input type="button" value="submit" onclick = "validateForm()"/></td>
</tr>
</table>
</form>
</div>
</body>
</html>
// in your validation JSON call you need to remove this line:
"category" : document.forms["myForm"]["category"].value,
// there is no category input from your form.  often js breaks if it cant find the item it is looking for, as is the case here.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.