Okay so basically I have a variable in ajax which I want to send to my socket.io server to check if the variable is already in a json array.
Ajax:
function isUniqueEmail(email) {
//email checking script here
$.get('info/mailcheck.js' + email, function(response) {
if(response == 1) {
alert("Your email is already on our list");
}
else {
alert("We will add you shortly");
};
})
};
the json array:
{"mail":
[
"tom#gmail.com",
"fred#gmail.com",
"bob#gmail.com"
]}
The socket.io part is where im confused. Basically it just needs to take the variable (an email) check if it is already in the array and return a 1 if it is or return a zero if not and write it in the array.
I don't know much about node.js but you could use a function a bit like the following to check if a value is in an array and push the value to it if it is not.
function pushToArray(value, array) {
function inArray(val, arr) {
for (var i=0; i<arr.length; i++) {
if (arr[i] == val) {
return true;
}
return false;
}
if (inArray(value, array) {
return 1;
} else {
array.push(value);
return 0;
}
}
Are you confused about how to get this to work with socket.io or just how to find the element in the array? If you're using node.js, just use the array.indexOf method:
//obj = {"mail": ["mail1#foo.com", "mail2#baz.com"]}
if (obj.mail.indexOf(email) != -1) {
//We have your email!
}
else {
//We don't
}
Related
I want to validate from and to text fields using codeigniter validation. I have created
validateSchedule function that will validate on callback but here validation is not
working it is working only for required condition.
public function validateSchedule()
{
$fromDate=$_POST['from_date'];
$toDate=$_POST['toDate'];
if(empty($toDate) || empty($fromDate))
{
return TRUE;
}
else
{
$diffNoof_days = 10;
if(strtotime($fromDate) > strtotime($toDate)){
$this->form_validation->set_message('validateSchedule','from_date_must_be_smaller_than_to_date');
return FALSE;
}else if(strtotime($fromDate) == strtotime($toDate)){
$this->form_validation->set_message('validateSchedule','from_date_to_must_not_be_same');
return FALSE;
}else if($diffNoof_days>10)
{
$this->form_validation->set_message('validateSchedule','duration_should_not_exceed_10_days');
return FALSE;
}
}
}
$this->form_validation->set_rules('from_date','From Date','trim|required');
$this->form_validation->set_rules('to_date','To Date','trim|required|callback_validateSchedule');
You don't show the actual callback, so I'm speculating that you have named the method wrong by not removing the callback_ prefix. In other words, the definition
public callback_validateSchedule($str)
{
...
}
should be
public validateSchedule($str)
{
...
}
If I have guessed wrong please show the actual code for validateSchedule()
I'm showing an array with items of type 'request' in a table. I want to sort the columns of the table so I planned to make a click method for every column header. This methods sorts the array based on the value of the property shown in that column.
public sortProduct(): void {
this.requests.sort((a, b) => {
if (a.productName < b.productName)
return -1;
if (a.productName > b.productName)
return 1;
return 0;
});
if (!this.productSortOrder) {
this.requests.reverse();
this.productSortOrder = true;
} else {
this.productSortOrder = false;
}
}
This works, but now I need to make a method for every column. I am looking for a way to call a sort method like this:
this.requests.sortMethod(property, order);
This method would then sort the requests array based on the property of the objects in the array and in the given sortorder.
How can I do that? I guess I'm looking for something like Func<> in C#.
You can us a function signature for a similar effect to Func
sortProduct<T>(prop: (c: Product) => T, order: "ASC" | "DESC"): void {
this.requests.sort((a, b) => {
if (prop(a) < prop(b))
return -1;
if (prop(a) > prop(b))
return 1;
return 0;
});
if (order === "DESC") {
this.requests.reverse();
this.productSortOrder = true;
} else {
this.productSortOrder = false;
}
}
// Usage
sortProduct(p=> p.productName, "ASC");
Or you can use the property name instead (keyof Product will ensure the string must be a property of Product):
sortProduct<T>(propName: keyof Product, order: "ASC" | "DESC"): void {
this.requests.sort((a, b) => {
if (a[propName] < b[propName])
return -1;
if (a[propName] > b[propName])
return 1;
return 0;
});
...
}
// Usage
sortProduct("productName", "ASC");
sortProduct("productName_", "ASC"); // Error
You can use a SortUtil class with a static template method sortByProperty:
export class SortUtil {
static sortByProperty<T>(array: T[], propName: keyof T, order: 'ASC' | 'DESC'): void {
array.sort((a, b) => {
if (a[propName] < b[propName]) {
return -1;
}
if (a[propName] > b[propName]) {
return 1;
}
return 0;
});
if (order === 'DESC') {
array.reverse();
}
}
}
Can someone explain this solution to me? A friend helped me, but he just wrote it all out and didn't explain it. Now, I'm really confused :(
_.indexOf = function(array, target){
var result = -1;
_.each(array, function(item, index) {
if (item === target && result === -1) {
result = index;
}
});
return result;
};
return result;
};
The function traverses through all the elements of the array and returns index of the first element that is equal to target. The code could also look like this:
_.indexOf = function(array, target){
var result = -1;
_.each(array, function(item, index) {
if (item === target) {
result = index;
return false;
}
});
return result;
}
I'm trying to save an object in IsolatedStorageSettings to save the high scores for my game, but whenever I try to save an updated copy of the object C# seems to think the object hasn't changed. I tried creating a custom Equals function for the HighScores class but that doesn't seem to help.
Any idea what I'm doing wrong?
Thanks
public bool AddOrUpdateValue(string Key, Object value)
{
bool valueChanged = false;
// If the key exists
if (isolatedStore.Contains(Key))
{
// If the value has changed
if (isolatedStore[Key] != value) //This keeps returning false
{
// Store the new value
isolatedStore[Key] = value;
valueChanged = true;
}
}
// Otherwise create the key.
else
{
isolatedStore.Add(Key, value);
valueChanged = true;
}
return valueChanged;
}
//This is located inside the HighScores class
public bool Equals(HighScores newHighScores)
{
for (int i = 0; i < highScores.Length; i++)
{
if (!highScores[i].Name.Equals(newHighScores.GetIndex(i).Name))
{
return false;
}
if (!highScores[i].Time.Equals(newHighScores.GetIndex(i).Time))
{
return false;
}
}
return true;
}
You haven't implemented the equality operators '==' and '!=' and these compare reference equality, you are going to have provide the implementation which maps on to your 'Equals' method
http://msdn.microsoft.com/en-us/library/ms173147%28v=vs.80%29.aspx
You should do isolatedStore.Save() to commit the changes
Below is my code which I am trying to find 'Username Ok' in the respone text, and then flag UserNameOk as true. I cannot get this working.
function check_username(username)
{
var httpRequest;
make_request()
function stateck()
{
if(httpxml.readyState==4)
{
if (httpxml.responseText == "Username Ok")
{
UserNameOk = true;
}
else
{
UserNameOk = false;
}
checkCanSubmit();
document.getElementById("user_div").innerHTML=httpxml.responseText;
}
}
httpxml.onreadystatechange=stateck;
user_url="ajax_username.php?username=" + username.value;
httpxml.open("GET",user_url,true);
httpxml.send(null);
}
Below is my checkCanSubmit code
function checkCanSubmit()
{
if (UserNameOk && PasswordOk && EmailOk)
{
document.getElementById("button").disabled= false;
}
else
{
document.getElementById("button").disabled= true;
}
}
Any help will be appreciated Thanks.
Just a guess: the response might be something like "Username Ok\n" (the '\n' being a newline character which you can't see). Or it might have whitespace at the beginning or end. You could print out (alert) the length of the string to test that.
Are you seeing the correct response come in through firebug?
First step is to make sure your server is returning the correct response by looking at firebug. I might also simplify the code to help narrow down the issues. Try:
function check_username(username)
{
var httpRequest;
make_request()
function stateck()
{
if(httpxml.readyState==4)
{
if (httpxml.responseText == "Username Ok")
{
alert('1');
UserNameOk = true;
}
else
{
alert('2');
UserNameOk = false;
}
}
}
httpxml.onreadystatechange=stateck;
user_url="ajax_username.php?username=" + username.value;
httpxml.open("GET",user_url,true);
httpxml.send(null);
Can you be more specific about where the problem is?