By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,050 Members | 1,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,050 IT Pros & Developers. It's quick & easy.

Validation for Alphabets

nitindel
P: 67
Hi All,

I have a text box and want to validate it like this:

I want to enter Numerics or alphanumerics in the text box but not Alphabets alone.If Alphabets alone entered then Error Should be thrown.

Please tell...Its very very Urgent...

Thnaks in Advance,


Nitin Sharma
Software Engineer
Dec 20 '07 #1
Share this Question
Share on Google+
17 Replies


gits
Expert Mod 5K+
P: 5,389
hi ...

play with this:

Expand|Select|Wrap|Line Numbers
  1. // val is your input-value here :)
  2. var val = 'foo';
  3.  
  4. if (!(/\d/g.test(val))) {
  5.     alert('put at least one numeric char in :\)');
  6. }
  7.  
it uses a regExp for the purpose :)

kind regards
Dec 20 '07 #2

nitindel
P: 67
hi ...

play with this:

Expand|Select|Wrap|Line Numbers
  1. // val is your input-value here :)
  2. var val = 'foo';
  3.  
  4. if (!(/\d/g.test(val))) {
  5.     alert('put at least one numeric char in :\)');
  6. }
  7.  
it uses a regExp for the purpose :)

kind regards


Please tell clearly dear..!

I want to have alert + action should also not get performed..
I am running update query..So it should not get fired wen getting alert neither it should postback.
but with your script ,,the query is firing..
Dec 20 '07 #3

gits
Expert Mod 5K+
P: 5,389
it is clear ... according to your first requirement :) ...

please post the client side code you are using and where the check is performed. i guess to avoid the submit then your checkfunction should return false in addition to the alert and otherwise it should return true ... but i should see how you use the check at the moment ...

kind regards
Dec 20 '07 #4

nitindel
P: 67
Hi All,
I am working on ASP,VBScript,Javascript.

I need a validation in a textBox in which i can enter Numerics,alphanumerics but not Alphabets alone.
i.e if alphabets alone are entered it should give alert that "Only Alphabets are not allowed..".

Please provide me a Solution.i am looking for it for a long time ...


Thanks & Regards
Nitin Sharma
Software Engineer
Dec 26 '07 #5

nitindel
P: 67
Hi ,
Please tell.i am using this code...Please reply to the question soon..!!!

Expand|Select|Wrap|Line Numbers
  1. var num_valid="0123456789";
  2.  
  3.     for (var i=0; i<document.test.employeeid.value.length; i++) 
  4.     {
  5.         if (num_valid.indexOf(m.charAt(i)) < 0 )
  6.          {
  7.             alert('Only Alphabets not allowed..!! \n Please enter an alphanumeric/Numeric  values ');
  8.             document.test.employeeid.focus();
  9.             return false;
  10.         }
  11.     }
Dec 26 '07 #6

gits
Expert Mod 5K+
P: 5,389
hi ...

you may simply use a regExp:

Expand|Select|Wrap|Line Numbers
  1. function test_value(val) {
  2.     var re = /\d/g;
  3.     return re.test(val);
  4. }
  5.  
  6. var val = 'abcd1';
  7.  
  8. alert( test_value(val) );
  9.  
kind regards
Dec 26 '07 #7

nitindel
P: 67
hi ...

you may simply use a rexExp:

Expand|Select|Wrap|Line Numbers
  1. function test_value(val) {
  2.     var re = /\d/g;
  3.     return re.test(val);
  4. }
  5.  
  6. var val = 'abcd1';
  7.  
  8. alert( test_value(val) );
  9.  
kind regards


Nopes...
It is not at all working...I think u didn't get my Question....
I am saying that it should allow Alphanumerics, Numerics but not ALPHABETS alone...
Please read the question carefully...
Dec 27 '07 #8

gits
Expert Mod 5K+
P: 5,389
that is exactly what it does ... read the code carefully ... the function returns false in case there is no numeric character in it ...
Dec 27 '07 #9

nitindel
P: 67
My Cool Dude... Please read Carefully now.....
I have a text box in an ASP Form...ok..

Now i have to enter employee ID there...which can be Numeric ,alpha numeric...as well....

There is a module in a page that Changes the Employee ID ....okies....

Now.. i want that if EmployeeID is changed in the TextBox..it should not accept only ALPHABETS ALPHABETS ALPHABETS...when entered alone. like "Michael, Bryan, Sachin..etc etc..."

but it can accept entries like Michael123,Sachin007 and so on....

If entered alphabets alone ...it Should give alert that "Only alphabets can not be an EmployeeID"...

Thanks,
Nitin Sharma
Software Engineer
Dec 27 '07 #10

gits
Expert Mod 5K+
P: 5,389
please be careful with your words ... and your attitude ... i'll help you out since you asked for it but i will not do write the entire code for you!! i got your question two times (i've merged the threads) and answered it ... and you didn't get the answer two times ... did you try to understand it? tried to use the code?

the following example shows you how to use the code ...

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function test_value(val) {
  3.     var re = /\d/g;
  4.  
  5.     return re.test(val);
  6. }
  7.  
  8. function validate(node) {
  9.     var val = test_value(node.value);
  10.  
  11.     if (!val) {
  12.         alert('you did it really wrong');
  13.  
  14.         node.focus();
  15.         node.select();
  16.     }
  17.  
  18.     return val;
  19. }
  20. </script>
  21. <body>
  22.     <input type="text" onchange="validate(this);"/>
  23. </body>
  24.  
as you can see ... quite trivial and it does exactly what you requested. so in case you don't know how to use provided snippets then simply ask for it ...

regards
Dec 27 '07 #11

gits
Expert Mod 5K+
P: 5,389
threads merged ...

MOD
Dec 27 '07 #12

nitindel
P: 67
Hi All,
Greetings..i found the solution ..!!!


the snippet is :

Expand|Select|Wrap|Line Numbers
  1. var regexNum = /\d/;
  2. var regexLetter = /[a-zA-z]/;
  3. if(!regexNum.test(m) &&  regexLetter.test(m)){
  4.     alert('Only Alphabets are not allowed..!!');
  5.     return false;
  6. }
Note: var m = document.test.employeeid.value;

Coooool one :-)

Thanks
Nitin Sharma
Software Engineer
Dec 28 '07 #13

gits
Expert Mod 5K+
P: 5,389
hi ...

glad to hear that but one thing to mention. your test returns true in case the value would be:

Expand|Select|Wrap|Line Numbers
  1. m = '...';
for example. so it would be better to use an OR in the check like the following:

Expand|Select|Wrap|Line Numbers
  1. var re1 = /\W/g;
  2. var re2 = /\d/g;
  3.  
  4. if(re1.test(m) || !re2.test(m)) {
  5.     alert(
  6.         'Special Characters or Only Alphabets\n'
  7.         + 'are not allowed..!!'
  8.     );
  9. }
  10.  
that would only allow a-z, A-Z, _ and 0-9 and requires at least one numeric character ...

kind regards
Dec 28 '07 #14

P: 13
please be careful with your words ... and your attitude ... i'll help you out since you asked for it but i will not do write the entire code for you!! i got your question two times (i've merged the threads) and answered it ... and you didn't get the answer two times ... did you try to understand it? tried to use the code?

the following example shows you how to use the code ...

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function test_value(val) {
  3.     var re = /\d/g;
  4.  
  5.     return re.test(val);
  6. }
  7.  
  8. function validate(node) {
  9.     var val = test_value(node.value);
  10.  
  11.     if (!val) {
  12.         alert('you did it really wrong');
  13.  
  14.         node.focus();
  15.         node.select();
  16.     }
  17.  
  18.     return val;
  19. }
  20. </script>
  21. <body>
  22.     <input type="text" onchange="validate(this);"/>
  23. </body>
  24.  
as you can see ... quite trivial and it does exactly what you requested. so in case you don't know how to use provided snippets then simply ask for it ...

regards

gits,
Hatsoff for ur patience and attitude to help others.
Dec 28 '07 #15

nitindel
P: 67
hi ...

glad to hear that but one thing to mention. your test returns true in case the value would be:

Expand|Select|Wrap|Line Numbers
  1. m = '...';
for example. so it would be better to use an OR in the check like the following:

Expand|Select|Wrap|Line Numbers
  1. var re1 = /\W/g;
  2. var re2 = /\d/g;
  3.  
  4. if(re1.test(m) || !re2.test(m)) {
  5.     alert(
  6.         'Special Characters or Only Alphabets\n'
  7.         + 'are not allowed..!!'
  8.     );
  9. }
  10.  
that would only allow a-z, A-Z, _ and 0-9 and requires at least one numeric character ...

kind regards


Nopes...
Pipe(||) (OR) is not fitted here..i have tried and verified ...
the script is running wonderfully...
&
Thanks to you also...for helping me...

bbye nd tc
Nitin Sharma
Software Engineer
Dec 31 '07 #16

gits
Expert Mod 5K+
P: 5,389
as i said ... with the posted script the user could enter '...' as employeeID ... but in case that is ok ... then do it the way you did ... so i consider that thread to be resolved ...

kind regards
Dec 31 '07 #17

nitindel
P: 67
as i said ... with the posted script the user could enter '...' as employeeID ... but in case that is ok ... then do it the way you did ... so i consider that thread to be resolved ...

kind regards

Ya thread is closed/resoved...

the expressions that u are telling ..for that i have already a check for Regular Expression/Special Characters and Blank spaces before the upper snippet..!!!

So its done..

Thanks..!!!

Nitin Sharma
Dec 31 '07 #18

Post your reply

Sign in to post your reply or Sign up for a free account.