.NET Logo
Welcome Guest Search | Active Topics | Members | Log In | Register

สอนวิธีเขียน ValidationExpression หน่อยครับ Options · View
chanachonp
Posted: Friday, September 12, 2008 11:53:05 AM
Rank: มือสมัครเล่น
Groups: Member

Joined: 12/2/2007
Posts: 26
Location: Nonthaburi

อยากล๊อคเฉพาะตัวอักษร แต่ห้ามมีคำว่า นาย หรือ นาง นำหน้า ต้องเขียนยังไงครับ

paedotnet
Posted: Wednesday, September 17, 2008 9:47:34 PM

Rank: มือเทพ
Groups: Member

Joined: 12/6/2007
Posts: 354
Location: bkk

ในตัวอย่างนี้จะมี TextBox กับ button
ในเขียน javascript ดังนี้


var stringfilter=new Array("นาย","นาง");
var countword=0;
function FilterTextFunction()
{
 var str_textbox=document.form1.TextBox1.value;//รับค่าจาก TextBox1
 for(var i=0; i<stringfilter.length; i++)//วนลูปค่าที่จะนำมา filter ในที่นี้คือ นาย กับ นาง
 {
     for(var j=0; j<(str_textbox.length); j++)//วนลูปค่าใน TextBox1
     {
       if(stringfilter[i]==str_textbox.substring(j,(j+stringfilter[i].length)))//ตรวจสอบค่าว่าถ้าเท่ากับค่าที่กำหนดไว้(นาย,นาง) ก็ให้เพื่อค่า countword
         countword++;
     }
 }
 if(countword>0)//ถ้ามีคำว่านายหรือนางก็ให้ขึ้นเตือน alert
 {
alert("ไม่ต้องใส่คำว่า นายหรือนาง");
 }
}

ตรง Button ใส่ Properties ดังนี้


OnClientClick="FilterTextFunction();"

ตัวอย่างโค้ดทั้งหมด

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
var stringfilter=new Array("นาย","นาง");//ข้อความที่กำหนดสำหรับ Filter
var countword=0;
function FilterTextFunction()
{
 var str_textbox=document.form1.TextBox1.value;//รับค่าจาก TextBox1
 for(var i=0; i<stringfilter.length; i++)//วนลูปค่าที่จะนำมา filter ในที่นี้คือ นาย กับ นาง
 {
     for(var j=0; j<(str_textbox.length); j++)//วนลูปค่าใน TextBox1
     {
       if(stringfilter[i]==str_textbox.substring(j,(j+stringfilter[i].length)))//ตรวจสอบค่าว่าถ้าเท่ากับค่าที่กำหนดไว้(นาย,นาง) ก็ให้เพื่อค่า countword
         countword++;
     }
 }
 if(countword>0)//ถ้ามีคำว่านายหรือนางก็ให้ขึ้นเตือน alert
 {
alert("ไม่ต้องใส่คำว่า นายหรือนาง");
 }
}
</script>
</head>
<body>
    <form id="form1" name="form1" runat="server">
    <div>
   
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Submit"  OnClientClick="FilterTextFunction();" />
     </div>
    </form>
</body>
</html>


ผลลัพธ์ถ้าใส่คำว่านาย หรือนางใน TextBox ก็จะขึ้น เตือนดังรูป


 



[With great power comes great responsibility]
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFVision Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.


Sponsored by