最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript in jsficefaces - Stack Overflow

programmeradmin4浏览0评论

I have file with jspx extension i write javascript like

function isNumber(inputId){

            var value = document.getElementById('mainForm:'+ inputId).value;
            var  s = value.length;

            while(s >= 0){
                var c = value.charAt(s);
                if(c > "9"){
                    alert("Value must be digit");
                    document.getElementById('mainForm:'+ inputId).value = "";
                    document.getElementById('mainForm:'+ inputId).focus();
                    return false;
                }
                s --;
            }
            return true;
        }

that work perfect but when i check if condition like if(c > "9" || c < "0") it will gives error like

.sun.facelets.FaceletException: Error Parsing /WEB-INF/includes/templates/cc-classic-template.jspx: Error Traced[line: 386] The content of elements must consist of well-formed character data or markup.

After long observation i found that <(less than) sign will create problem. Is JSF not support < sign ?

I have file with jspx extension i write javascript like

function isNumber(inputId){

            var value = document.getElementById('mainForm:'+ inputId).value;
            var  s = value.length;

            while(s >= 0){
                var c = value.charAt(s);
                if(c > "9"){
                    alert("Value must be digit");
                    document.getElementById('mainForm:'+ inputId).value = "";
                    document.getElementById('mainForm:'+ inputId).focus();
                    return false;
                }
                s --;
            }
            return true;
        }

that work perfect but when i check if condition like if(c > "9" || c < "0") it will gives error like

.sun.facelets.FaceletException: Error Parsing /WEB-INF/includes/templates/cc-classic-template.jspx: Error Traced[line: 386] The content of elements must consist of well-formed character data or markup.

After long observation i found that <(less than) sign will create problem. Is JSF not support < sign ?

Share Improve this question asked Mar 25, 2011 at 6:52 chetanchetan 3,25520 gold badges73 silver badges114 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 11

Enclose your Javascript in CDATA Sections:

<script language="javascript" type="text/javascript">
/* <![CDATA[ */

    function isNumber(inputId){

            var value = document.getElementById('mainForm:'+ inputId).value;
            var  s = value.length;

            while(s >= 0){
                var c = value.charAt(s);
                if(c > "9"){
                    alert("Value must be digit");
                    document.getElementById('mainForm:'+ inputId).value = "";
                    document.getElementById('mainForm:'+ inputId).focus();
                    return false;
                }
                s --;
            }
            return true;
        }

        //Code containing "<" also es in this section

/* ]]> */
</script>

As explained by Matt Handy, you could not use the < or > sign in your JSPX, as this is a XML format. You have three solutions regarding your problem:

  • Escape by using &lt; or &gt;.
  • Use <![CDATA[ ... ]]> to hold your JavaScript code in your page.
  • Set your JavaScript code in a separate .js file, and load it in your JSPX page.

Besides the escaping and CDATA answers:

If you want to check if a value is a number, there is a javascript built-in function for that: isNaN

Here is an example:

if (isNaN(document.getElementById('mainForm:'+ inputId).value))
  {
    alert("Please enter digits");
    document.getElementById(obj).focus();
    document.getElementById(obj).select();
    return false;
  }
发布评论

评论列表(0)

  1. 暂无评论