Click here to Skip to main content
16,021,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi I want to create a text box which take special character as input and it can take number and character also but it should take only one special character. if the special character count includes more than 1 then it should alert a message after alerting it should maintain the same text box but it should remove the special character which was entered second time. i have succeed in writing code were it will allow only one special character but after that it is displaying the second special character also i don't want that. the code is show below please help for my assignment



XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default7.aspx.cs" Inherits="Default7" %>
<html>
<head>
<script>
    function abc(value) {
        var count = 0;
        var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
        for (var i = 0; i < value.length; i++) {

            if (iChars.indexOf(value.charAt(i)) != -1) {
                count++;

            }
        }

        if (count == 1)
            return true;


        else {

            if (count > 1)
                alert("u cannot enter more than one special charecter");
            Txt1.value = "";
            return false;

        }
    }

</script>
</head>
<body>

<input type="text" id="Txt1" onkeyup="abc(this.value)"/>
<br/>
<input type="button" value="save" id="btn"/>

</body>


</html>
Posted
Comments
Mohibur Rashid 9-Oct-12 20:23pm    
you don't even have time to write you in your alert

1 solution

I've modified your code, check if this help you...

JavaScript
function abc(value) {
            var count = 0;
            var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
            var text = value;
            var array = value.split("");
            for (var i = 0; i < array.length; i++ ) {
                if (iChars.indexOf(array[i]) != - 1) {
                    count++;
                    if (count == 2) {
                        text = value.substr(0, value.lastIndexOf(array[i]));
                        break;
                    }
                }
            }
            if (count == 2) {
                alert("u cannot enter more than one special charecter");
            }
            document.getElementById("Txt1").value = text;
        }
 
Share this answer
 
Comments
syedmas 10-Oct-12 23:48pm    
Thank you so much carlos.ma for your effort yo answer my question, it is working fine but still i have one small query . Why is it allowing 2 special character. I want it to just allow only one special character and trim of the next special character after alerting the message. i would be thankfull if you help me out with it and give a bit of explaination. thank you so much!!!!!!!
carlos.ma 11-Oct-12 10:50am    
Ok, no problem, I'll explain you what I did here. I think the key point is when the count var value is 2, if this happens then it means that our value has at least 2 special characters so there is no need to continue the search; that's why the code just trims the content of the value from the begin to the appearance of the second special character. The code uses lastIndexOf instead of indexOf because if the second special character was the same that the first one then it would trim the value from the begin to the apperance of the first special character; for instance let's say that our value was "myname//", then using indexOf the result would be "myname", but with lastIndexOf the result will be "myname/". I hope I've made a good explanation, however if you have any doubt or comment let me know and I'll check it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900