I want to create a function that will validate a string for improper chars.

As a first try, i tried it on numbers. It Works on IE9, but not on Chrome 12.0.742.122 and Firefox 5 & 6

<html>
    <head>
        <title>Regex Example</title>
        <script type="text/javascript">
            var regexp;
            var input;
            var output;

            function replaceChars()
            {
                regexp = "";
                output = "";
                input = document.getElementById("myinput").getAttribute("value");
                regexp = /\d+/g;
                output = input.replace(regexp, "");//Remove Digits
                setOutput();
            }

            function setOutput()
            {
                document.getElementById("myoutput").setAttribute("value", output);
                document.getElementById("myexpr").setAttribute("value", regexp);
                document.getElementById("myinput").select();
            }
        </script>
    </head>

    <body>
        Enter : <input type="text" id="myinput" value="" maxlength="25" size="25"/><br/>
        RegEx : <input type="text" id="myexpr" value="" maxlength="25" size="25" readonly /><br/>
        Output : <input type="text" id="myoutput" value="" maxlength="25" size="25" readonly />
        <input type="button" #nClick="replaceChars();" value="Remove Digits"/>
    </body>
</html>
share|improve this question
feedback

3 Answers

and also u used

onClick instead of #nClick
<input type="button" #nClick="replaceChars();" value="Remove Digits"/>
share|improve this answer
feedback

First, it is onclick and not #nClick.

Also, instead of getAttribute/setAttribute, you can set attributes directly like this:

input = document.getElementById("myinput").value;

and

document.getElementById("myoutput").value = output;

Then it works fine on Chrome: http://jsfiddle.net/Ta6Lw/.

share|improve this answer
feedback
 regexp = /\w+/g; remove character
regexp = /\w+/i;  remove all character lower case and uppercase
regexp = /\D+/i;  remove Non digits u can use any thing u wish...
output = input.replace(regexp, "");//Remove Digits
share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.