PDA

View Full Version : how to validate a textarea for valid html


medi
12-31-2002, 10:25 AM
i wont to allow user to type a html in my textfield and textareas.
but how to validate it, to make sure that a user didnt make an error? ok i have my html-buttons(b,i,a href, ...) beside so user kann klick on it and the script inserts the html in textarea. BUT user can still make it wrong by changing the format. the only secure way ist to call a validating function onSubmit to check for errors.I searched by many engines with terms like
javascript + "html validator"
hmlparser javascript
but nothing helped

DOES ANYBODY KNOW FOR ALREADY WRITTEN SCRIPT WHICH DO EXACTLY THAT?

i started to write my own but im to bed programmer! :)
here it is. if u can improve it pleas contakt me
///////////////////////////
var lastEndTagPos=0;
var allTextTag="";
function checkMarkup(text){ // wird onSubmit aufgerufen
reg_s=new Array(); //opening-Tag array
reg_e=new Array(); //closeing-Tag array
reg_s[0]=/{b}/;
reg_e[0]=/{\/b}/;
reg_s[1]=/{i}/;
reg_e[1]=/{\/i}/;
reg_s[2]=/{a href=\'mailto:\w.+?\'}/;
reg_e[2]=/{\/a}/;
reg_s[3]=/{ol}/;
reg_e[3]=/{\/ol}/;
reg_s[4]=/{ul type=\'\w.+?\'}/;
reg_e[4]=/{\/ul}/;
reg_s[5]=/{dl}/;
reg_e[5]=/{\/dl}/;
reg_s[6]=/{li}/;
reg_e[6]=/{\/li}/;

var args=arguments.length;
if(lastEndTagPos==0) {
allTextTag=text;
}

for(var i=0;i<reg_s.length;i++){ //traverse all
//{todo}reg goes der array-reihe, nicht den ersten gefundenen in text
m_s=text.match(reg_s[i]);
m_e=text.match(reg_e[i]);
s_s=text.search(reg_s[i]);
s_e=text.search(reg_e[i]);
if(s_s!=-1) break; //if first match find, break
}
if(s_s!=-1 && s_e!=-1){ //if opening-Tag closed properly
l=m_s[0].length;
l_e=m_e[0].length;
innerText=text.substring((s_s+l), s_e); //content of actual Markup
if(args==1) {
lastEndTagPos=(s_e+l_e); //position where lasst parent-end-tag found
}
alert("start="+(s_s)+"\nend="+ s_e +"\n\ninner="+innerText+"\n\n übergebener text ist \n"+text+"\n\nlastEndTagPos ist "+lastEndTagPos);
if(isMarkupUsed(innerText)){ //if there are inner markups
checkMarkup(innerText,"used");
}else{
text=allTextTag.substring(lastEndTagPos);
//{todo}das kann ich aber nicht beim ersten aufruf machen
nextTag=text.search(/{\w[ \w].*?}/);
text=text.substring(0,nextTag);
alert(text);
allTextTag=text;
checkMarkup(text); //search for following Markups,from actual end-tag
}
alert("ok");
}else{
alert("missing closeing "+m_s[0]+" Tag!");
}
}
/////////////