1

I want a javascript validation on a textbox which can accept.

a. Characters
b. Characters with Numbers

It should not accept only numbers

For example i want something like:-

Abc123, A7 organisation.

but I dont want like;-

333333 , 2222222.

Also, special characters are also not allowed

I tried like below js function but it is not working

function NumersWithCharValidation() {
    var textBoxvalue = document.getElementById('txtNameOfFirm').value;
    alert(textBoxvalue);
    if (textBoxvalue.match("/^(?![0-9]*$)[a-zA-Z0-9]+$/")) {
        alert('Good to go');
    }
    else {
        alert('Only Numbers are not allowed');
    } 
}

<input id="txtNameOfFirm" runat="server" onkeypress="return NumersWithCharValidation();" maxlength="200" type="text" width="65%" />

kindly suggest what is wrong

Peter Szekeli
  • 2,712
  • 3
  • 30
  • 44
Nad
  • 4,605
  • 11
  • 71
  • 160

3 Answers3

2

change the method to

function NumersWithCharValidation(thisObj) {
    var textBoxvalue = thisObj.value;
    if ( textBoxvalue.length > 0 && isNaN( textBoxvalue ) && !textBoxvalue.match( /\W/ ) ) 
    {
        alert('Good to go');
    }
    else 
    {
        alert('Only Numbers are not allowed. Special characters are also not allowed' );
    } 
}

<input id="txtNameOfFirm" runat="server" onkeypress="return NumersWithCharValidation(this);" maxlength="200" type="text" width="65%" />

isNaN( "textBoxvalue" ) will check if the value is a pure number

textBoxvalue.match( /\W/ ) checks if there is any special character in the value

gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • @gurvinder372 you have to remove the quotes from the check in isNaN – eltonkamami Feb 15 '16 at 08:21
  • @coder there is a regex `/\W/` is a regex that matches non-alpha-numeric characters. Did you try the code? – gurvinder372 Feb 15 '16 at 08:28
  • @gurvinder372: yes i tried it. it seems its working for me. but one thing i want to ask, it repeatedly gives me alert even when there is no text entered into the textbox. why ? – Nad Feb 15 '16 at 08:30
  • @coder added the condition to check for empty string as well. – gurvinder372 Feb 15 '16 at 08:34
  • @gurvinder372: No, that is not what I want. See. when I go to add alphabet for the first time in the textbox, it straight away go into the else part and gives error as`alert('Only Numbers are not allowed. Special characters are also not allowed');` why ? does it is related to `onkeypress` thing ?? – Nad Feb 15 '16 at 08:39
  • @coder `it is related to onkeypress thing` yes, if you don't want to be bothered with every keypress then use **onblur** so that only when you tab out of the input box the validation will be done – gurvinder372 Feb 15 '16 at 08:40
  • superb. working fine..Accepting the answer :) Thanks a lot – Nad Feb 15 '16 at 08:43
  • hi @gurvinder372: are you their?? just one small doubt – Nad Feb 15 '16 at 10:43
  • i am still getting the _else part error_, when I just keep mouse and take out the mouse without inserting any text on **onblur** why ? – Nad Feb 15 '16 at 11:33
  • @coder in that case don't put `textBoxvalue.length > 0 &&` in the if condition since that is what giving this condition – gurvinder372 Feb 15 '16 at 11:35
  • should I remove and check ?? – Nad Feb 15 '16 at 11:35
  • @coder yes, please do it and let me know if problem still persists – gurvinder372 Feb 15 '16 at 11:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/103482/discussion-between-coder-and-gurvinder372). – Nad Feb 15 '16 at 11:37
  • yes problem is still their, can u come on chat so that I can explain u in detail – Nad Feb 15 '16 at 11:39
  • @gurvinder372: hi, are you their ? just need one small change here in the regex. – Nad Feb 24 '16 at 05:49
  • after space when i put numbers it is not giving me error. Like `121212 12121` why ?? – Nad Feb 24 '16 at 06:01
  • any help @gurvinder372 ?? – Nad Feb 24 '16 at 06:15
  • @coder gimme some time, can you share a fiddle in the meantime? – gurvinder372 Feb 24 '16 at 06:24
  • sure, waiting for ur answer. i guess you understand the logic ??. yeah i will share the fiddle in 2mins – Nad Feb 24 '16 at 06:26
  • @coder you made a simple error, you need to chose 'Wrap in head' rather than `onload` setting in jsfiddle. – gurvinder372 Feb 24 '16 at 07:11
  • @gurvinder372: ok, i just added the code and gave u. kindly help what is wrong – Nad Feb 24 '16 at 07:13
  • @gurvinder372: it is still taking `23432 423423` like this, which it should not – Nad Feb 24 '16 at 08:15
  • @coder but `23432 423423` satisfies the critieria you have mentioned in your OP. – gurvinder372 Feb 24 '16 at 09:22
  • @gurvinder372: no, i dont want only numbers. have a look at the question. I want `abc3 test` but not `1212 3434` – Nad Feb 24 '16 at 09:26
  • @gurvinder372: when I add text like `abc test test` it stilll gives me error, WHY ?? – Nad Feb 24 '16 at 11:37
0

How about a regex like this? Positive lookahead for letters, it works for your example inputs.

if (textBoxvalue.match("/^(?=[a-zA-Z]+)[\w\s,]*$/")) { alert('Good to go'); } else { alert('Only Numbers are not allowed'); }

buræquete
  • 14,226
  • 4
  • 44
  • 89
0

This will not allow the user to type any further

jQuery("#txtNameOfFirm").keypress(function (event, ui)
{ return NumersWithCharValidation(event) });

and

function NumersWithCharValidation(evt) {

    if (jQuery("#txtNameOfFirm").val().match(/^[0-9]+$/) == null)
    { return true; } else { return false; }
}