Nov 24

Display Arabic properly in your ASPX pages

Posted By Ahmed El-Kilani On 24 Nov 2007 2 Comments »

One of the problems that beginners to web hosting face when publishing Arabic contents is false encoding to charecters existing in .aspx or .ascx files.
Because of the default encoding in your system is Arabic visual studio IDE automatically saves web files into windows-1256 encoding, but webhost server does not usually has Arabic (windows-1256) as the default encoding. So each request or response to web files hosted on such hosts will be wrongly encoded.
To solve this issue we have two options:

1- Fortunetly ASP .Net can force encoding of requests and responses to any web application by adding the following line to the system.web section in the Web.config file:

<globalization requestEncoding="windows-1256" responseEncoding="windows-1256" fileEncoding="windows-1256"/>

But this solution will not work if this encoding is not supported by the server machine, so if not working go to the next option.

2- Make visual studio IDE save your files into unicode encoding, because unicode charecters are 2 bytes instead of 1, so unicode will work properly with any language. To do so with visual studio; from File menu click Save As:
 
Choose UTF-8 without signature from the encoding combo; then save the file.

Nov 16

Best thing I like about jquery is each() function which loops the jquery elements and do the best for you. For code reusability I'm posting this article to show how with one javascript function we could confirm that all our required fields are not empty when submitting the form.

First say we have a registration form for reservation application, we have some fields must be requied, others must only contains a valid email address and others are optional. So without considering any calling for our confirm function we will only mark out input fields. Here is how to do:

<input type=text runat=server alt=required />
<
input type=text runat=server alt=email />
<
input type=text runat=server />


The first input is required, the second one must contain a valid email address while the last is optional.

Now in our javascript file , lay out these few lines of code:

$(document).ready(function(){
$(
'form').onsubmit(function(){
    
var success = true;
    
$('input[alt=required]').each(function(){
    
if($(this).val() == ''){
    success 
= false;
    
}
})
;

if
(!sucess)
    
alert('Fill in all required fields before submitting.');

return 
validateEmails();
});
});

function 
validateEmails()
{
    
var success = true;
    var 
emailFilter /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    
$('input[alt=email]').each(function(){
    
if(!emailFilter.test($(this).val()) ){
    success 
= false;
    
}

    
if(!success)
    
alert('You must enter a valid email address');

    return 
success;
}


Save this file and link it to any page you wanna validate before submitting and don't forget linking to jquery source.

Cool isn't it?