1

I am trying to validate my form using Selenium ID but it's not passing the first name. I need it to only accept first name in letters no numbers. Below is the code:

function validateFrName(){

        if (frName.val() == "1"){
         frName.addClass("error");
        $("#firstNameErrorMsg").text("First name can only contain letters.");
        $("#firstNameErrorMsg").addClass("error");
        return false;

    } else if (frName.val().length <3){
        frName.addClass("error");
        $("#firstNameErrorMsg").text("Please enter your first name.");
        $("#firstNameErrorMsg").addClass("error");
        return false;
    }else{
        frName.removeClass("error");
        $("#firstNameErrorMsg").text("OK");
        $("#firstNameErrorMsg").removeClass("error");
        return true;
    }
}
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179

2 Answers2

0

I didnt understand your question correctly. If you get your firstname via this "frName.val()", then why couldnt you try checking frName.val().substring(0,2)= 1 or 2 or 3 etc. Please tell me some more detail if you didnt get with this..

Please try this also

function valid()
{
var k=true;
for(i=0;i<frName.val().length-1;i++)
{
if ( frName.val().substring(i,i+1)=0 ||   frName.val().substring(i,i+1)=1)
{
k=false;
}
}
return k;
}
BINU VARGHESE
  • 364
  • 1
  • 4
  • 16
  • Hello, the first If statement is just a mess around hence the matching to "1". I need some way to make it not accept e.g 1Steve or steve1 just letters only no numbers. Please try and help with easy coding like used above. Thanks – Leon Smith Oct 12 '12 at 16:42
  • I am sorry for that misunderstanding. frName.val() will return your first name. correct? – BINU VARGHESE Oct 12 '12 at 16:55
  • i am abit confused as its my first time using jquery. How can i implement the code above into mine? sorry for being a pain! – Leon Smith Oct 12 '12 at 17:25
0

See this jsFiddle http://jsfiddle.net/vHzKn/1/

The problem is with how you are grabbing the frName.

I have added a variable called firstName and assigned it the value of the textbox with the id of frName

var firstName = $("#frName").val();

I then validate based on the firstName variable.

Please note that the code sample in the jsfiddle doesn't find all numbers etc... that is a separate question. You should probably use a javascript validation library for that.

EDIT: Look at Check whether an input string contains number to find a regex to determine if a number exists in a string.

See this super simple jsfiddle with a jquery example: http://jsfiddle.net/XCYL7/

Community
  • 1
  • 1
Nathan Koop
  • 24,803
  • 25
  • 90
  • 125