textbox validation using javascript [Resolved]

Posted by Kranthi under ASP.NET on 11/11/2010 | Points: 10 | Views : 19512 | Status : [Member] | Replies : 8
i have one textbox for name.it contains alphabetics only...it should not accept whitespace at starting it accept white space after entering any alphabets(examble-hai welcome) using javascript




Responses

Posted by: T.saravanan on: 11/11/2010 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
Hi,

Small changes in my code.

function clickButton1()

{
var sText = document.getElementId("txtText").value;
if (event.keyCode == 32 && sText !='')
{
return true;
}
else if (event.keyCode < 97 || event.keyCode > 122)
{
alert("Enter Characters Only");
return false;
}
}


Try the above code.Here txtText is your input value.

My suggestion is don't expect exact answer.Refer the code for your guidance and then try your self.

Cheers :)

Thanks,
T.Saravanan

Kranthi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Raja on: 11/11/2010 [Member] Starter | Points: 25

Up
0
Down
User event.KeyCode to find out the code of space and ignore that. Look at the below link that contains several code snippet related with JavaScript that may help you.

http://www.dotnetfunda.com/codes/showcatcodes.aspx?start=60&page=6&category=57

Thanks

Regards,
Raja, USA

Kranthi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kranthi on: 11/11/2010 [Member] Starter | Points: 25

Up
0
Down
i use this but it doesnt accept whitespace......the keycode for whitespace is 37 where i put that

function clickButton1(e1)
{
if (navigator.appName.indexOf("Microsoft Internet Explorer") > (-1))
{
if (event.keyCode < 97 || event.keyCode > 122)
{
alert("Enter Characters Only");
return false;
}
}
}

Kranthi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: T.saravanan on: 11/11/2010 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi,

If allow white space in your textbox means using this script. The keycode for whitespace is 32

function clickButton1()

{
if (event.keyCode == 32)
{
return true;
}
else if (event.keyCode < 97 || event.keyCode > 122)
{
alert("Enter Characters Only");
return false;
}
}


For your reference...
http://www.geekpedia.com/tutorial138_Get-key-press-event-using-JavaScript.html

I think its your requirement.

Cheers :)

Thanks,
T.Saravanan

Kranthi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kranthi on: 11/11/2010 [Member] Starter | Points: 25

Up
0
Down
my concept is it accept whitespace at middle only.....does not at the start of the charecter......example-kranthi b, but your given code is accept white space at starting

Kranthi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dora743 on: 11/22/2010 [Member] Starter | Points: 25

Up
0
Down
A sample one

(\w+)\s+\1

Dorababu

Kranthi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chamila on: 3/10/2011 [Member] Starter | Points: 25

Up
0
Down
Check this out for More Validations :
http://chamilaw.blogspot.com/2011/03/java-script-validations.html


Kranthi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Madhu.b.rokkam on: 3/10/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
function validate1() {

var len = document.getElementById("TextBox1").value.length;
if (event.keyCode == 32 && len == 0) {
document.getElementById("TextBox1").value = "";
alert("Cannot start with a space");
return false;
}
else if ((event.keyCode > 96 && event.keyCode < 122) || (event.keyCode > 64 && event.keyCode < 92) || event.keyCode == 32) {
return true;
}
else {
event.returnValue = false;
return false;
}
}


Add this in key press event of text box ...

Thanks and Regards
Madhu

Kranthi, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response