Saturday, 31 August 2013

Make a textbox generate text files like t1.txt t2.txt t3.txt, and so on

Make a textbox generate text files like t1.txt t2.txt t3.txt, and so on

I have this code on my website:
<form name="form" method="post">
<input type="text" name="text_box" size="50"/>
<input type="submit" id="search-submit" value="submit" />
</form>
<?php
if(isset($_POST['text_box'])) { //only do file operations when
appropriate
$a = $_POST['text_box'];
$myFile = "t.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $a);
fclose($fh);
}
?>
The thing i'd like it to do is that when there is already a t.txt, it'll
create a t2.txt, and then t3.txt and so on, so it doesen't overwrite the
text in the previous t.txt.

Friday, 30 August 2013

jQuery regex validation of e-mail address including blank value

jQuery regex validation of e-mail address including blank value

I am using custom[email] rule for email validation in Jquery Validation
Engine. What I want is, I want regex that validates email with blank value
also. If value is blank then also it should show error message. I dont
want to use required rule.
Here is the custom rule given in Jquery Validation Engine
"email": {
// HTML5 compatible email regex (
http://www.whatwg.org/specs/web-apps/current-work/multipage/states-of-the-type-attribute.html#
e-mail-state-%28type=email%29 )
"regex":
/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
"alertText": "* Invalid email address"
}
Please help me out.

Thursday, 29 August 2013

How can I get which 'IF' condition makes loop true with OR & AND?

How can I get which 'IF' condition makes loop true with OR & AND?

assume if i have code like:
if(condition1 || condition2 || condition 3 || condition4)
{
// this inner part will executed if one of the condition true. Now I want
to know by which condition this part is executed.
}
else
{
}

Wednesday, 28 August 2013

Devise update password

Devise update password

I want to change edit password process. On edit password page I have one
field :foo and I want to check if user correctly entered it.
I created PasswordsController and override update method, where I added
code for testing if :foo is same as in database:
def update
self.resource = resource_class.reset_password_by_token(resource_params)
# this is code that I added
unless self.resource.foo == params[resource_name][:foo]
self.resource.errors.add(:foo, "Foo not correct")
end
if resource.errors.empty?
resource.unlock_access! if unlockable?(resource)
flash_message = resource.active_for_authentication? ? :updated :
:updated_not_active
set_flash_message(:notice, flash_message) if is_navigational_format?
sign_in(resource_name, resource)
respond_with resource, :location =>
after_resetting_password_path_for(resource)
else
binding.pry
respond_with resource
end
end
Suppose I enter "test" in :foo field, but in database it's value is
"fake". It will print error on view ("Foo not correct") but it will
populate :foo field with value from database ("fake") not with value I
entered ("test").
I should redefine reset_password_by_token or use my custom method, but
wonder if there is more elegant way to solve this problem?

Data not appending to element in Handlebars.js

Data not appending to element in Handlebars.js

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"
type="text/javascript" charset="utf-8"></script>
<script
src="http://cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.0.beta.6.js"
type="text/javascript" charset="utf-8"></script>
<script src="app.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<ul>
<script id ="ajaxTemplate" type="text/x-handlebars-template">
{{#each}}
<li>
<span class="meta">{{name}} on </p>
</li>
{{/each}}
</script>
</ul>
<script>
$(document).ready(function(){
//document.write("Hello");
var data = [{
"name":"Name2",
"date":"12/12/1999"
}, {
"name":"Name1",
"date":"12/12/1999"
}]
var source = $.trim($('#ajaxTemplate').html());
var template = Handlebars.compile(source);
var html = template(data);
//document.write(html);
$('ul').append(html);
});
</script>
</body>
</html>
Can anyone tell me what is the mistake in the above code. I am not able to
append the compiled JS code to DOM.

Qt Validator Regular expression

Qt Validator Regular expression

I'm trying to validate entry into a QLineEdit and I have my syntax wrong
somewhere because when I run my program it doesn't allow me to enter
anything in the edit box. The entry needs to be 3 uppercase alphabetic
characters, followed by 1,2 or 3, and then a further 2 digits and then one
final character alphabetic or digit.
QRegExp StudentForm::modCodeFormat("(\\s{3}\\d[123]\\d{2}\\w{1})");

What exactly is the difference between has_many, has_many_and_belongs_to_many and embeds_many in mongoid?

What exactly is the difference between has_many,
has_many_and_belongs_to_many and embeds_many in mongoid?

I understand this not a programming problem, I am unable to find a very
clear and descriptive solution.