www.webdeveloper.com
Results 1 to 9 of 9

Thread: validate and replace double quotes??

  1. #1
    Join Date
    Dec 2002
    Posts
    359

    validate and replace double quotes??

    i want to be able to have a JS function that i can call that would check if there was a quote or double quote in a textarea on my form.

    if there is one, i would like to replace it with either \" or \'.

    this would prevent my servlet from cutting of the endof the info (i think).

    if anyone can help out, i'd greatly appreaciate it!!
    Thanks!

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    2,653
    the method below should do it for you..
    Khalid

    Code:
    <form name="form1">
    	<textarea name="tarea"></textarea>
    	<input type="Button" value="process" onclick="process();"></input>
    </form>
    <script language="JavaScript1.2">
    function process(){
    	var data = document.form1.tarea.value;
    	alert("b4 processing \n"+data);
    	data = data.replace(/\"/g,"\\\"");
    	data = data.replace(/\'/g,"\\\'");
    	alert("data after replacements \n"+data);
    }
    </script>

  3. #3
    Join Date
    Dec 2002
    Posts
    359
    Thanks!!! It worked for the double quotes perfectly!!! but it didn't work for the single quotes for some reason.

    i have one more thing to add:

    if i want to validate 2 textareas, is there an easy way to alter the code?

    THANKS A LOT!!!!

    oh, it looks like u intended a message to alert the user to the change? if so, that part didn't work..
    no biggie!
    Last edited by sanjuT; 01-15-2003 at 04:35 PM.

  4. #4
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    2,653
    code works perfectly in ns6+ IE,you must have lost something in copy and pasting


    2 textareas

    Code:
    <body>
    <form name="form1">
    	<textarea name="tarea1"></textarea>
    	<textarea name="tarea2"></textarea>
    	<input type="Button" value="process" onclick="process();"></input>
    </form>
    <script language="JavaScript1.2">
    function process(){
    	var data1 = parseData(document.form1.tarea1.value);//text area 1
    	var data2 = parseData(document.form1.tarea2.value);//text area 2
    	alert("data after replacements \n\tData1\n"+data1+"\n\tData1\n"+data2);
    }
    
    function parseData(data){
    	data = data.replace(/\"/g,"\\\"");
    	data = data.replace(/\'/g,"\\\'");
    	return data;
    }
    </script>

  5. #5
    Join Date
    Dec 2002
    Posts
    359
    not sure where i'm messing it up.

    if i'm not getting a message box when i submit, does that mean the function is not triggering?

    here's my form tag:

    <form name="rfss" method="post" action="/servlet/RFSSConfirm"
    onClick="return process();"
    onSubmit=" this.SystemID.optional = true;

    return validate(this);">

    i have renamed the form in the scripts..

    not sure if u are familiar with this, but here's where the data from the 2 textareas are used in my servlet:

    msgText1= msgText1 + "<input type = hidden name = Description value = " + req.getParameter("Description") + ">";
    msgText1= msgText1 + "<input type = hidden name = Benefits value = " + req.getParameter("Benefits") + ">";


    here's the text i have been using to test in each textarea:

    testing "the" form 'for' quotes'

    and finally, here's the code u supplied that i used (i changed a few things to work in my form):

    <script language="JavaScript1.2">
    function process(){
    var data1 = parseData(document.rfss.Description.value);//text area 1
    var data2 = parseData(document.rfss.Benefits.value);//text area 2
    //alert("data after replacements \n\tData1\n"+data1+"\n\tData1\n"+data2);
    }

    function parseData(data){
    data = data.replace(/\"/g,"\\\"");
    data = data.replace(/'/g,"\\'");
    return data;
    }
    </script>



    does this script rename the form data? (ie instead of Descriptions and Benefits, r they renamed to data1 and data2)?

    THANKS A LOT AGAIN!!! you have been helping me out quite a bit, and i really appreaciate it!
    Last edited by sanjuT; 01-16-2003 at 11:16 AM.

  6. #6
    Join Date
    Jan 2003
    Posts
    628
    Your question:
    if i want to validate 2 textareas, is there an easy way to alter the code?
    When you want to use the same function for more than one element ...
    You can tell which element called the function as follows:
    var Sender = event.srcElement;
    You then use the variable in your code so you can use the function for as many elements as you like.
    I have taken the liberty of using khalidali63's code already posted.

    e.g.

    function process(){
    var Sender = event.srcElement;
    var data = Sender.value;
    alert("b4 processing \n"+data);
    data = data.replace(/\"/g,"\\\"");
    data = data.replace(/'/g,"\\'");
    alert("data after replacements \n"+data);
    Sender.value = data;
    }

  7. #7
    Join Date
    Dec 2002
    Posts
    359
    ok, I'M REALLY STUMPED.. (oops CAPS lock)

    i'm not sure what i'm doing wrong.

    Is there a way for the servlet to fix the quote problem, or is it a better idea to do it client-side?

    there are a total of 3 servlets.

    1. the Confirm servlet - The whole string is fine, no cut-offs. takes info from HTML form and displays it.

    2. Auth servlet - takes info from Confirm page and creates a webpage with the form data in it. This is where i get the problem.

    3. the Send servlet - final submission of the data. Again, since it is taking the data from the Auth page, it is cut-off.


    Does this make any sense??

    I am going bonkers!!!

    Thanks all!

  8. #8
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    2,653
    can you post a link of actually not working page,so that I or some one else might be able to take a look.
    And for the second part of your question,
    yes you should be able to use escape characters in Java as well as in JavaScript.

    Or may be if you can not put link here,email me the exct page that is not working and a test string as well.


    Khalid

  9. #9
    Join Date
    Mar 2013
    Posts
    2
    Hi, I want to be able to have a JS function that I can call to check if there was a double quote in a form field.

    Is that possible too?

    Thanks,

    Sumacasu

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles