1

I want to use a jQuery/JavaScript function to validate the input of a textbox.

I have pairs of checkboxes like this:

<input type="text" id="MinMondayTxt" />
<input type="text" id="MondayTxt" />

this is for each day of the week (MinTuesdatTxt, MinWednesdayTxt...)

The Function is like this:

function ValidateTxt(TxtId){
}

It take the Id of a textbox as input. What I want to do is the following:

1) Check if the Id starts with "Min"

2) Get the corresponding textbox, by removing "Min" from the id. for example if we have MinMondayTxt I want to remove the "Min" in order to access the MondayTxt textbox

3) Compare the the values of the 2 textboxes (all values are numeric)

4) if MinMondayTxt > MondayTxt Alert the user

I am looking for help for step 1) and 2) : How can I check if the id starts with "Min" and how to get the corresponding textbox id (by removing the min)

3 Answers 3

2

You can use replace() method with the #id selector of jquery as below

function ValidateTxt(TxtId){ 
 var withoutminid = TxtId.replace(/^Min/, '');

 var withouminidval = $('#' + withoutminid).val();
 var minidval = $('#' + TxtId).val();
} 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer. Can you tell me how to check if TxtId starts with Min, before replacing? because sometimes the function is called for textboxes that does not start with "Min" and I don't want to do anything for them
1

This will remove "Min" if present, then fetch the jQuery object:

$('#' + TxtId.replace(/^Min/, ''))

1 Comment

Missing the initial '#' for an ID selector.
1

maybe you could try with split:

txtId.split('Min');

if Min exist it should create an array like that :

array = ["Min","MondayTxt"];

then you just have to use array[1].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.