Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript
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

 

<%@ 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 9-Oct-12 2:49am
syedmas402
Comments
Mohibur Rashid at 9-Oct-12 20:23pm
   
you don't even have time to write you in your alert

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I've modified your code, check if this help you...
 
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;
        }
  Permalink  
Comments
syedmas at 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 at 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)

  Print Answers RSS
0 OriginalGriff 364
1 Nirav Prabtani 268
2 _Amy 230
3 Sergey Alexandrovich Kryukov 171
4 CHill60 170
0 OriginalGriff 8,004
1 Sergey Alexandrovich Kryukov 7,017
2 Maciej Los 4,039
3 Peter Leow 3,738
4 CHill60 2,912


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 10 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100