Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Tuesday, 14 August 2012

Replace characters in javascript

Replace characters in javascript

In server side we have "Replace" method to replace the characters in a string but on javascript replace method will be not useful in certain cases instead of that we can do for loop to replace the particular characters and get the specific output.
Example :
           var StringChar = "this is my 324o234u324t2342p234u234t"; //Your string to get replaced
         In this case i need to remove the numbers and get the filtered output.


        Replace Method in Javascript :
         var ReplacedCharacters = String.replace("0123456789"," ");
         Output :   "this is my 324o234u324t2342p234u234t"


         In this cases we can do like this :
         var ReplacedChar = "";
         var Characters = "";
         var InValidChars = "0123456789";
         for (d = 0; d < StringChar.length; d++) {
         Characters = StringChar.charAt(d);
         if (InValidChars.indexOf(Characters) == -1) {
         ReplacedChar = ReplacedChar + Characters;
         }
         the resulting output is :   ReplacedChar = this is my output

how to get sum of 2 or more text box values in javascript


ADD TWO OR MORE TEXT BOX VALUES IN JAVA SCRIPT

Generally we will add text box values  in java script as follows
              Test out with the 2 Text boxes , to see what yours should do. Enter a 2 in the first box, and a 2 in the second box. Then third Text Box will get updated by sum value.
       Here we are using key press event to validate and allow only numbers, Key Up event to sum of the values and update in third text box.
so to ADD two or More text box values in java script follow this code :-
JAVA SCRIPT
<script type="text/javascript" language="javascript">
 
        function AllowOnlyNumbers(obj, e) {
            var key = window.event ? e.keyCode : e.which;
            if ((key > 45 && key < 58) || key == 8) {
                if (key == 47) {
                    return false;
                }
            }
            else {
                return false;
            }
            if (obj.value == "") {
                if (key == 46) {
                    return false;
                }
            }
            var DecimalCount = 0;
            for (d = 0; d < obj.value.length; d++) {
                var Character = obj.value.charAt(d);
                if (Character == ".") {
                    DecimalCount++;
                }
            }
            if (key == 46) {
                if (DecimalCount == 1) {
                    return false;
                }
            }
        }
        function SumValue() {
            var txtbox1 = document.getElementById("TextBox1").value;
            if (txtbox1 == "" || txtbox1 == ".") {
                txtbox1 = 0;
            }
            var txtbox2 = document.getElementById("TextBox2").value;
            if (txtbox2 == "" || txtbox2 == ".") {
                txtbox2 = 0;
            }
            var txtbox3 = document.getElementById("TextBox3");
            txtbox3.value = parseFloat(txtbox1) + parseFloat(txtbox2);
        }
     
    </script>

ASPX PAGE

<div>
        <asp:TextBox ID="TextBox1" runat="server" onKeyUp="return SumValue();" onkeypress="return AllowOnlyNumbers(this,event);"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server" onKeyUp="return SumValue();" onkeypress="return AllowOnlyNumbers(this,event);"></asp:TextBox>
        <asp:TextBox ID="TextBox3" runat="server" onKeyUp="return SumValue();" onkeypress="return AllowOnlyNumbers(this,event);"></asp:TextBox>
    </div>

OUTPUT



Word Documents and Excel Documents in IFRAME


In this below example we are converting word OR excel document to HTML.

In this case we are converting and giving converted HTML file as ""src"" to ""iframe"".
Step By Step Procedure :

1)First step is create one folder in your application which needs to maintain the documents in that because iframe will take src within the application itself,outside of the application it will not work.
2) The aspx page as follows :


3) Add Interop.Excel & Interop.Word dll references for the application :

4) The button click event will be like this :
First get the binary data of file and save that file to its original format in documents folder.After saving the file according to the file extension convert to Html format and give the Html file as src for iframe.
private Word.ApplicationClass MSWord;       // The Interop Object for Word
private Excel.ApplicationClass MSExcel;     // The Interop Object for Excel
object Unknown = Type.Missing;        // For passing Empty values
protected void Page_Load(object sender, EventArgs e)
{
           
}

protected void btnView_Click(object sender, EventArgs e)
{
            byte[] BinaryFile = null;
            if (FileUpload2.PostedFile != null)
            {
                int FileSize = FileUpload2.PostedFile.ContentLength;
                BinaryFile = new byte[FileSize];
                FileUpload2.PostedFile.InputStream.Read(BinaryFile, 0, (int)FileUpload2.PostedFile.ContentLength);
                string FileName = FileUpload2.FileName;
                lblFileName.Text = FileName;
                string FileExtension = Path.GetExtension(FileName);
                string FolderPath = Server.MapPath("~/Documents");
                File.WriteAllBytes(FolderPath + "\\" + FileName, BinaryFile);
                if (FileExtension == ".doc" || FileExtension == ".docx")
                {
                    ConvertWordToHTML(FolderPath + "\\" + FileName, FolderPath + "\\" + FileName.Split('.')[0] + ".html");
                }
                else if (FileExtension == ".xls" || FileExtension == ".xlsx")
                {
                    ConvertExcelToHTML(FolderPath + "\\" + FileName, FolderPath + "\\" + FileName.Split('.')[0] + ".html");
                }
                docPreview.Attributes["src"] = "../Documents/" + FileName.Split('.')[0] + ".html";
            }
}

This method is used to convert WordDocument to HTML :

public void ConvertWordToHTML(object FilePath, object SaveTarget)
{
            if (MSWord == null)
                MSWord = new Word.ApplicationClass();

            try
            {
                MSWord.Visible = false;
                MSWord.Application.Visible = false;
                MSWord.WindowState = Word.WdWindowState.wdWindowStateMinimize;

                MSWord.Documents.Open(ref FilePath, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown, ref Unknown);

                object format = Word.WdSaveFormat.wdFormatHTML;

                MSWord.ActiveDocument.SaveAs(ref SaveTarget, ref format,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                       ref Unknown, ref Unknown);
            }
            catch (Exception e)
            {
            }
            finally
            {
                if (MSWord != null)
                {
                    MSWord.Documents.Close(ref Unknown, ref Unknown, ref Unknown);
                    MSWord.Quit(ref Unknown, ref Unknown, ref Unknown);
                }
            }
}

This method is used to convert ExcelDocument to HTML :

public void ConvertExcelToHTML(string Source, string Target)
{
            if (MSExcel == null) 
                MSExcel = new Excel.ApplicationClass();

            try
            {
                MSExcel.Visible = false;
                MSExcel.Application.Visible = false;
                MSExcel.WindowState = Excel.XlWindowState.xlMinimized;

                MSExcel.Workbooks.Open(Source, Unknown,
                     Unknown, Unknown, Unknown,
                     Unknown, Unknown, Unknown,
                     Unknown, Unknown, Unknown,
                     Unknown, Unknown, Unknown, Unknown);

                object format = Excel.XlFileFormat.xlHtml;

                MSExcel.Workbooks[1].SaveAs(Target, format,
                        Unknown, Unknown, Unknown,
                        Unknown, Excel.XlSaveAsAccessMode.xlExclusive, Unknown,
                        Unknown, Unknown, Unknown,
                        Unknown);
            }
            catch (Exception e)
            {
            }
            finally
            {
                if (MSExcel != null)
                {
                    MSExcel.Workbooks.Close();
                    MSExcel.Quit();
                }
            }
}

5) Here in my example i have taken fileupload to upload word and excel documents, after selecting a file by clicking View button we can view the output just like this :

Output for WordDocument :



Output for ExcelDocument :

Popular posts