Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6988845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:08:03+00:00 2026-05-27T19:08:03+00:00

I have the following simple Javascript code that allows only digits to be entered

  • 0

I have the following simple Javascript code that allows only digits to be entered into the specified TextField. I have used it earlier in many places such as Servlet, JSP, JSTL/EL and even in PHP and it worked fine as expected. In JSF however, I less concerned with Javascript in which some characters need to be escaped. I tried my best to get it run but I couldn’t. Here is the Javascript code along with JSF stuff.

<?xml version='1.0' encoding='UTF-8' ?>

<html xmlns="http://www.w3.org/1999/xhtml"
     xmlns:h="http://java.sun.com/jsf/html"
     xmlns:f="http://java.sun.com/jsf/core">

<h:head>
 <title>Demo</title>

 <script type="text/javascript" language="javascript">

     $(document).ready(function()
     {
         $("#myForm:txtDemo").keypress(function(event)
         {
             if (event.keyCode==46 || event.keyCode==8|| event.keyCode==9 || event.keyCode>=35 && event.keyCode<=40)
             {
                 // let it happen, don't do anything
             }
             else
             {
                 if (event.charCode<48 || event.charCode>57 || event.charCode==16)
                 {
                            event.preventDefault();
                 }
             }                    
         });
     });

 </script>

</h:head>

<h:body>
    <h:form id="myForm">

        <br/><br/><br/>
        <h:inputText id="txtDemo" required="true" requiredMessage="Mandatory." validatorMessage="The field should contain al least 10 digits">
            <f:validateLength maximum="10" minimum="2"/>
            <f:validateRegex pattern="[0-9]*"/>
            <f:ajax event="valueChange" execute="txtDemo msg" render="txtDemo msg"/>
        </h:inputText><br/>

        <h:message id="msg" for="txtDemo" showDetail="true" style="color:red"/><br/>

        <h:commandButton id="btnSubmit" value="Submit"/>

    </h:form>
</h:body>


Javascript is not being compiled. It parses the following error.

An Error Occurred:

Error Parsing /Restricted/TempTags.xhtml: Error Traced[line: 14] The entity name 
must immediately follow the '&' in the entity reference.

I have even placed the Javascript code in a separate js file and included that file in this page. I have also replaced <script></scrip> with <h:outputScript></h:outputScript> still the same problem remains.


I have also tried the following to see whether the Javascript function is being called or not using alert(); removing the error parsing code from the function but the function itself wasn’t even called and the alert message wasn’t displayed.

<script type="text/javascript" language="javascript">

     $(document).ready(function()
     {
         $("#myForm:txtDemo").keypress(function(event)
         {
             alert();
         });
     });

</script>

Which necessary changes are required in that Javascript to get it run as expected?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:08:04+00:00Added an answer on May 27, 2026 at 7:08 pm

    This is XHTML, so you need to either

    • Wrap the script contents in CDATA tags:

      <script>
      //<![CDATA[
      $(document).ready(function()
      {
          // snip...
      }
      //]]>
      </script>
      
    • Or, move the script to an external file, and include it using <h:outputScript />.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following (fairly) simple JavaScript snippet that I have wired into Greasemonkey.
I have a simple html/javascript based Android app that loads with the following code:
I have an javascript that I place into a page using the code below.
I have the following very simple Javascript-compatible regular expression: <script type=text/javascript id=(.+) src=([^]+)> I
We have the following simple Stored Procedure that runs as an overnight SQL server
I have the following simple python test script that uses Suds to call a
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
i have the following simple code, but it doesn,t work <ul> <li id=one onmouseover=this.style.background-color='white';>
as you can see in the following image, i have a simple form that
I have following simple class: @interface Article: NSObject { NSString *title; } @property (copy,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.