Restricting users to enter only numbers inside a text box แบบที่ 2

วิธีเขียน JavaScript กำหนดให้ User ป้อนข้อมูลที่เป็น ตัวเลขลงใน TextBox เท่านั้น (แบบที่ 2)

<HTML>
<HEAD>
<SCRIPT language=Javascript>
<!–
function isNumberKey(evt)

{

var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;

return true;

}
//–>
</SCRIPT>
</HEAD>
<BODY>
<INPUT id=”txtChar” onkeypress=”return isNumberKey(event)” type=”text” name=”txtChar”>
</BODY>
</HTML>

Restricting users to enter only numbers inside a text box

หลายครั้งที่ผมต้องเขียน WebForm แล้วต้องให้ User กรอกแต่ข้อมูลที่เป็นตัวเลขเท่านั้น พยายามใช้ ServerControl หลายครั้ง หรือไม่ก้อทำเป็น Ajax เพื่อให้เช็กที่ฝั่ง Server แต่ท้ายที่สุดก้อไม่ทันใจ User เลยต้องหันมาพึ่งพา JavaScript เหมือนเคย รวดเร็ว ทันใจ

วิธีเขียน JavaScript กำหนดให้ User ป้อนข้อมูลที่เป็น ตัวเลขลงใน TextBox เท่านั้น

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=“Content-Type” content=“text/html; charset=iso-8859-1″>
<meta http-equiv=“Content-Style-Type” content=“text/css”>
<meta http-equiv=“Content-Script-Type” content=“text/javascript”>

<script type=”text/JavaScript”>

function valid(f) {

if (!/^\d*$/.test(f.value)) {

alert(”Only integer numbers allowed!”);

f.value = f.value.replace(/[^\d]/g,”");

}

}

</script>

</head>
<body>

<input name=”" type=”text” onkeyup=”valid(this)”>

</body>
</html>

ยังมีอีกวิธีดูที่นี่เลยนะครับ -> http://blogs.monthon.com/2008/09/restricting-users-to-enter-only-numbers-inside-a-text-box-no2/

Linux: Find Files Containing Text

Because of our being forgetful in nature, we oftentimes forget the files that we have created. We can only be fortunate if we can still remember the path, folder, or directory where we have stored those missing files. If that’s the case, it would never be a big deal then. However, trully this a big problem and even painful if we can’t even remember where we have placed the missing files in our computer. “Simple,” I’m hearing you… “find it.” Yeah, we can find it. But wait, do you know the filename?

[ad#banner-with-link]

Forgetting files and forgetting the filenames are most common in us. I will never believe somebody out there have a photographic memory and has never experienced missing some of the files he had created before. For sure, we all have gone through that frightening experience especially if the file that is missing is so precious to us.

In Windows, this problem can easily be addressed just by using the find or search tool in the Start menu. Can you remember some texts or phrases in the filename? Use “find files with names” and unleash the power of the wildcard character (*). For example, if you can only remember the word “statistic” in the filename, then search for “*statistic*” and that will search for files with the word “statistic” in the filename. “I can’t even remember a word in the filename,” again I can hear you saying that. Well, I don’t think you can’t even remeber even a single word in the file content itself. If you can’t remember even a word or phrase in the filename then go for the file content itself. In Windows, still you can search for files containing some texts that you specify in your “find files containing text” input box. That will absolutely solve your problem of forgetting words in the filename itself.

However, if you are in Linux, the whole thing would be more different and complex than it is in Windows especially if you are just a normal user dependent on the GUI interface. Linux is more on executing commands from a shell.

So if you are a normal user and that you are facing the “missing files” problem in Linux, don’t worry, I will show you the most common methods in solving this issue:

Find files that contain a text string

grep -lir "text to find" *

The -l switch outputs only the names of files in which the text occurs (instead of each line containing the text), the -i switch ignores the case, and the -r descends into subdirectories.

Find files containing search terms on Ubuntu

To find files containing keywords, linux has a powerful command called grep, which you can use to find the lines inside any file or a list of files.

grep -i -n ‘text to search' *

List files containing text

Used to recursively search a directory for files containing a string, output the names of the files and the line number. This will search all regular files in for .

grep --with-filename --line-number `find -type f`

Submitted by: Selwin Verallo
Find more tips and techniques at Smart Pad.

วิธีการ add JavaScript ให้กับ Button ใน Asp.net

How to add JavaScript into Asp.net Button control to confirm user before doing the process

Html control :
<INPUT TYPE=“button” VALUE=“Button1″ ONCLICK=“if(confirm(’Do you want delete?’)){ return true; }else{ return false; }”>

.Net control :
Button1.attribute.Add(“onclick”,”if(confirm(’Do you want delete this item(s)?’)){ return true; }else{ return false; }”);

Have fun :)

How to Export DataTable to Spreadsheet using C#

วิธีการ Export ข้อมูลจาก DataTable ไปยัง Excel Spreadsheet ด้วย C#

public static void ExportToSpreadsheet(DataTable table, string name)
{

HttpContext context = HttpContext.Current;
context.Response.Clear();
foreach (DataColumn column in table.Columns)
{

context.Response.Write(column.ColumnName + “;”);

}
context.Response.Write(Environment.NewLine);
foreach (DataRow row in table.Rows)
{

for (int i = 0; i < table.Columns.Count; i++)
{

context.Response.Write(row[i].ToString().Replace(“;”, string.Empty) + “;”);

}
context.Response.Write(Environment.NewLine);

}

context.Response.ContentType = “text/csv”;
context.Response.AppendHeader(“Content-Disposition”, “attachment; filename=” + name + “.csv”);
context.Response.End();

}

// Using
ExportToSpreadsheet(table, “products”);

[ad#menu-link]