var el = document.getElementById("myButton");
var messageContainer = document.getElementById('messageContainer');
el.onclick = function() {
addUser(username, email, onSuccess);
}
var lastResponseCode, oldSendFunction;
// store the native send()
oldSendFunction = XMLHttpRequest.prototype.send;
// override the native send()
XMLHttpRequest.prototype.send = function() {
this.addEventListener("readystatechange", function(readyEvent) {
lastResponseCode = readyEvent.target.status;
console.log('readystatechange - set last response code:', lastResponseCode);
});
// call the native send()
oldSendFunction.apply(this, arguments);
}
function onSuccess(result) {
if (lastResponseCode == 200) {
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var username = document.getElementById("username").value;
var email = document.getElementById("email");
var message = document.getElementById("result").classList;
document.getElementById("users").innerHTML += '<li>' + username + '</li>';
if (!filter.test(email.value)) {
document.getElementById('result').innerHTML += 'Not a valid email';
email.focus;
return false;
} else {
//I could have cleared the contents of the 'result' span by using something similar to document.getElementById('result').innerHTML=' ' But I figured I'd have more control manipulating the span results using CSS. In this situation we may be better off using a Javascript library like prototype or jQuery, which have methods to do this, as well as functions that can first check if an element already has a class assigned.
message.add("hide");
}
}
else {
//handle response code other than 200 here
}
}
// Do not modify this function. Add user service wrapper.
function addUser(username, email, callback) {
var xhr = new XMLHttpRequest();
var response;
var success = (!!Math.round(Math.random()));
if (!success) {
response = JSON.stringify({
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!