I wrote this code for a sign up form. I want to add the values of email id and password to JSON file in the form of array objects.
Here is my html file.
<!DOCTYPE html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<meta name="robots" content="noindex, nofollow" charset="UTF-8">
</head>
<body>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.3.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.0/jquery.validate.min.js"></script>
<script type="text/javascript" src="jQuery-UI.js"></script>
<script src="validate.js"></script>
<form id="myform" enctype="application/json">
<div class="container-form">
<fieldset>
<legend>Sign up</legend>
<label for="username">User Name</label><br>
<input type="text" name="username[]" id="username" placeholder="Your name" minlength="2" required=""><br><br>
<label for="contact">Contact</label><br>
<input name="contact" type="number" id="contact" placeholder="Phone Number" >
<br><br>
<label for="emailid">Email id</label><br>
<input type="email" name="emailid[]" id="email" pattern="[^@]+@[^@]+\.[a-zA-Z]{2,6}" placeholder="Enter email id" required=""><br><br>
<label for="password">Password</label><br>
<input type="password" name="password[]" id="password" placeholder="Enter password" required="" minlength="6"><br><br>
<label for="password">Confirm Password</label><br>
<input type="password" name="cpassword" id="cpassword" placeholder="Confirm Password"><br><br>
<button type="submit" name="register" id="register" value="Submit">Submit</button>
<button type="reset" value="Clear">Clear</button>
<div id="result">
</div>
</fieldset>
</div>
</form>
</body>
my submit.js which has been converted into bundle.j since require() cannot be used directly. here is my submit.js
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var fs = require('fs');
$(function() {
$(document).ready(function(){
$("#myform").validate({
rules: {
password: "required",
cpassword: {
required: true,
equalTo: "#password"
}
},
messages: {
cpassword: {
required: 'Please re-enter the password',
equalTo: 'Please enter the same password'
}
}
});
$("#register").on('click', function(){
var data1 = fs.readFileSync('data.json');
var words = JSON.parse(data1);
function make_json(form){
var jsonObject = {
"name" : $("#username").val(),
"email" : $("#email").val(),
"password": $("#password").val()
};
console.log(jsonObject);
};
var data2 = JSON.stringify(jsonObject, null, 2);
fs.writeFile("./data.json", data2, finished);
function finished(err) {
console.log('all set.');
}
});
});
});
},{"fs":2}],2:[function(require,module,exports){
},{}]},{},[1]);
Where am I going wrong? If possible send the edited code instead of link to some website. Thanks.