Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Check Form Content for HTML Before Submit

Posted on 2007-11-14
4
Medium Priority
?
1,430 Views
Last Modified: 2012-06-27
I need some code that will check all the fields on a booking form to make sure they don't contain HTML before the form is submitted. In other words, if any field on the form contains < or > then I don't want the form to be submitted.

Can you please provide code.
0
Comment
Question by:slamhound
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
gnoon earned 2000 total points
ID: 20286575
<script language="javascript">

function check(theForm) {
  for(var i=0; i<theForm.elements.length; i++) {
    var element = theForm.elements[i];
    if(/<(\S+).*>(.*)<\/\1>/.test(element.value)) {
      alert('A html tag is detected!');
      element.focus();
      return false;
    }
  }
  return true;
}

</script>

<form action="..." name="booking" onsubmit="return check(this)">

Hope this help.
0
 
LVL 16

Expert Comment

by:gnoon
ID: 20286630
Sorry, it should be

if(/<*.*>*/.test(element.value)) {
0
 
LVL 14

Expert Comment

by:cezarF
ID: 20286658
try this to loop thru all inputs and not all form elements ...

<script>
function checkHTML(){
    var inputs = document.getElementsByTagName('input');
      for(var k=0;k<inputs.length;k++){
            var input = inputs[k]
            if(input.type == 'text'){
                  if (input.value.infexOf(">") > -1 || input.value.infexOf("<") > -1){
                        return false;
                  }
            }
      }
      return true;
}
</script>
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20287356
I hope you aren't just depending on Javascript.  You should make sure you have server script to do this too.

The best would be to have the javascript search the specific fields.  However if you want script that won't care and look at any fields then ...

function checkFields(frm) {
      for (var i=0; i<frm.elements.length; i++) {
            if (frm.elements[i].type == 'text' || frm.elements[i].type == 'textarea') {
                  frm.elements[i].value = frm.elements[i].value.replace(/<[^>]+>/g, "");
            }
      }
}

You should run the function as part of the submit process.  Your form tag can look like ...

<form [your attributes] onsubmit="checkFields(this);">

This will search text inputs and textareas.  You can add other field types if needed but that should cover them.  Also it just removes the html but will still allow the form to be submitted.  If you'd rather reject it then let me know.

Let me know if you have a question or need more info.

bol
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question