문제

I have a dropdown in which I am having

"-- Select--"

as the first item. I want to check if user has choosen something else from the dropdown instead of Select. So is there a way around to match if !-- Select --. I have done this till now.

  "regex": /^[!-- Select --]$/,

Where "!" means when not Select. I am achieving this client side.

도움이 되었습니까?

해결책

You can try this regex:

/^(!?--\s*[Ss]elect\s*--)$/

You should also find SelectedIndex property to evaluate this item.

다른 팁

It doesn't work because you're using a big character class; instead of trying to match the opposite, try to match what you don't want and then logically reverse that.

/^[-]{2} Select [-]{2}$/
function selectavalue() {
var selectBox = document.getElementById('selectboxid');
var user_input = selectBox.options[selectBox.selectedIndex].innerHTML;
var str = user_input;

if((str.match(/--Select--/i)) ) 
{
    alert("Please select a value");
} 
else 
{
    alert("The value u selected is"+str);
}
}

call this function from your selectbox.onchange="return selectavalue();"

You can use jQuery

if($("#id of your select box").attr("selectedIndex") == 0) 
{
     alert("You haven't selected anything!");
}

You have to make first value as default value.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top