Skip to main content

HTML INPUT enhancement with Regular Expressions and Java Scripts

අද කථා කරන්න යන්නේ බොහොම සරල දෙයක් ගැන. සරල උනාට ඉතින් මට මේක කරගන්න ඕන වෙලා හොයන කොට නම් ඉතින් හොයාගන්න ටිකක් කරදර උනා, ඒ නිසාම තමා මම හිතුවේ මේක ගැන ලියන්න ඕනෙ කියල. අපි හැමෝම HTML forms validate කරන්න Java Script පාවිච්චි කරනවා නේ, එක අලුත් දෙයක් නෙමේ. ඒ වගේම තමා Regular Expressions කියන්නෙත් අපිට අලුත් දෙයක් නෙමේ. අපි බලමු මේ දෙකම පාවිච්චි කරලා HTML textbox එකකට අපිට ඕනෙ characters විතරක් enter කරන්න දෙන්නේ කොහොමද කියල.

මෙන්න මේක තමා අපි පාවිච්චි කරන HTML page එක.

 <!DOCTYPE html>  
 <html>  
      <head>  
           <title>JS and RegEx</title>  
      </head>  
      <body>  
           <label for="txtUsername">Username</lable>  
           <input type="text" id="txtUsername" placeholder="Enter Username"/>  
      </body>  
 </html>  

මේ තියෙන page එක html විදිහට save කරලා browser එකේ බැලුවම මෙන්න මේ වගේ තමා පෙන්නේ.

දැන් හිතන්න අපිට මේ username එකට @,#,%,$ වගේ special characters නැතුව enter කරන්න දෙන්නේ a-z,A-Z,0-9 විතරයි කියලා.

අපිට පුළුවන් මේකට ලියන්න Reg Ex එකක්, මෙන්න මේ වගේ
"^[a-zA-Z0-9]"
 මේ පහල තියෙන්නේ අපි පාවිච්චි කරන Java Script function එක,
 function ValidateInput(evt)  
 {  
   var valRegExp = new RegExp("^[a-zA-Z0-9]");  
   if (valRegExp.test(String.fromCharCode(evt.which)))  
   {  
       return true;  
   }  
   else  
   {  
       return false;  
   }  
 }  

මෙතන ඔයාලට පේනවා ඇති අපි RegExp type එකේ object එකක් හදාගෙන තියෙනවා එකට අපි pass කරලා තියෙනවා අපේ regular expression එක,

ඒ වගේම තවත් වැදගත් දෙයක් තමා අපි test method එකට යවල තියෙන්නේ String.fromCharCode(evt.which) කියලා, ඒ මොකද evt.which කිව්වම අපිට එන්නේ අපි press කරපු key එකේ ASCII code එක. ඉතින් අපි ඒක අපිට අවශ්‍ය character එකට convert කරලා තමා ගන්නේ.

පහල තියෙන්නේ මේකට ඕනෙ කරන සම්පූර්ණ code එක.

 <!DOCTYPE html>  
 <html>  
      <head>  
           <title>JS and RegEx</title>  
           <script type="text/javascript">  
                function ValidateInput(evt)  
                {  
                  var valRegExp = new RegExp("^[a-zA-Z0-9]");  
                  if (valRegExp.test(String.fromCharCode(evt.which)))  
                  {  
                      return true;  
                  }  
                  else  
                  {  
                      return false;  
                  }  
                }  
           </script>  
      </head>  
      <body>  
           <label for="txtUsername">Username</lable>  
           <input type="text" id="txtUsername" placeholder="Enter Username" onkeypress="return ValidateInput(event)"/>  
      </body>  
 </html>  

ඔන්න ඕක තමා ඉතින් අද කතන්දරේ.

Comments

Popular posts from this blog

Adding Unique Constraints with Entity Framework Code First

Entity Framework Code First is a great way to define and maintain your database within your application it self. While it poses a nice set of complementing libraries like Data Annotations and Fluent Configurations which helps you specially in defining phase of your database, it would give you a headache if you try to define a unique constraint on a column. For example in the users table of your application you could probably have an int ID column which would serve as the primary key and you might need to make your Username column a unique one. Since you are using EF Code First you will soon find out there is no direct way to accomplish this requirement. Unfortunately fluent configurations doesn't have syntax like HasUnique(u => u.Username); I asked the same question in stackoverflow , but didn't get a convincing answer. Since there is no direct support from EF for this you could take one of following alternatives to achieve it. Approach 1 :  ...

Applying Themes for Silverlight 4

ටික දවසකට පස්සේ ලියන්න හොඳ දෙයක් එක්ක වෙලාවක් හම්බ උනා. මේ දවස් වල මම පොඩි අත්හදා බැලීමක් කරනවා Silverlight 4 එක්ක. Visual Studio 2010 තමා පාවිච්චි කරන්නේ. හැබැයි එක හිතපු තරම් ලේසි උනේ නෑ. ඒ නිසා මම හිතුවා ඒ ගැන පොඩ්ඩක් ලියන්න. මුලින්ම කියන්න තියෙන්නේ Silverlight 4 එක්ක වැඩ කරන්න නම් VS 2010 විතරක් තිබුනට මදි, ඒකට ඔයාලට ඕනෙ වෙනවා VS2010 Service Pack 1. ඒක download කරගන්න පුළුවන් මෙන්න මේ ලින්ක් එකෙන්. මෙතන ඇත්තටම තියෙන්නේ ISO image එකක්, ඒක mount කරලා install කළා නම් වැඩේ ගොඩ. ඊට පස්සේ අපි බලමු කොහෙන්ද මේ Themes හොයාගන්න පුළුවන් කියල. ඒකට යන්න මෙන්න මේ ලින්ක් ඒකට, එතන තියෙන " SL4Themes-templates.zip " කියන file එක download කරගන්න. මේ themes Microsoft එකෙන් officially release කරලා තියෙන themes වගයක්. ඇත්තටම මේ download එකේ තියෙන්නේ Project Templates. දැන් කරන්න තියෙන්නේ මේක Unzip කරලා VisualStudio2010 කියන folder එක ඇතුලේ තියෙන JetPack.vsix වගේ file එකක් double click කරන එක. එකෙන් පොඩි Installer එකක් දුවලා install වෙනවා Visual Studion Project Template එකක්. දැන් ක...

How to set a web page to the center of the browser

අද අපි කතා කරන්න ලැස්ති වෙන්නේ අපිට web page එකක් design කරනකොට ඕනේ වෙන පොඩි technique එකක් ගැන. ඔයාල ඕනේ තරම් දැකල ඇති web sites වල මුළු page එකම center වෙලා තියෙනවා. අපි උදාහරණයක්‌ ගත්තොත් W3Schools site එක වගේ. පේනවා නේ site එකේ content එක page එකේ මැදට වෙන්න තියෙන ලස්සන. මේක කරගන්න පුළුවන් මෙන්න මේ විදිහට. <html> <head> <title>Testing Centered Content</title> </head> <body> <div id="mainContent" style="width:900px;margin-left:auto; margin-right:auto;background-color:Gray;height:500px"> This is the content which is centered </div> </body> </html> මෙතන අපි ඇත්තටම විශේෂ දේවල් පාවිච්චි කරලා නෑ. සරල html code එකක් තමා තියෙන්නේ. අපි <div> එකක් හදාගෙන තියෙනවා mainContent කියල. ඒ div ඒකට style එකක් apply කරල තියෙනවා. එකේ අපි කරලා තියෙන්නේ site එකේ content width එක සෙට් කරලා තියෙනවා pixel 900 කට. ඊට පස්සේ වැදගත් වෙන්නේ margin-left එකයි margi...