jeudi 3 avril 2014

Adding the users and validating the email using json ajax


Vote count:

0




I am trying to add the users to userslist and validating email before adding to the list. When I click add user I am getting unexpected identifier. I have a form wrapped up with input username and email field


I am not able to validate my email field..


code here



var userData = [];
var addUserBtn = document.getElementById("add-user");
var userNameField = document.getElementById("username");
var emailField = document.getElementById("email");
addUserBtn.addEventListener('click', function(){
var newUser = {};
newUser.username = userNameField.value.trim();
newUser.email = emailField.value.trim();
userData.push(newUser);
if(emailField.value != ''){
reg = /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/;
if (reg.test(emailField.value) == true) {
validation.style.display ="none";
emailField.classList.remove("errorclass");
return false;
}
else {
emailField.setAttribute('class','errorclass');
validation.innerHTML = "please enter a valid email address";
validation.style.display ="block";

return false;
}
}
$.ajax({
url: '/echo/json/',
dataType: 'json',
data: {
json: JSON.stringify(userData),
delay: 3
},
success: function(data) {
for (var i = 0; i < data.length; i++) {
var li = $('<li>').text(data[i].username +" "+ data[i].email);
$('#users').append(li);
}

userNameField.value = '';
emailField.value = '';
},
error : function(){
var li = $('<li class="error">').text(response.error);
$('#users').append('<li class="error">Oups, something went wrong!</li>');
}
type: 'POST'
});

});


asked 1 min ago






Aucun commentaire:

Enregistrer un commentaire