简体   繁体   中英

Is there any way to make prompts not require a case sensitive answer ?

I am having a URL parameter like this .

/?name=John&accountID=123456789&email=jdoe@tc.com

When the page reloads a prompt box appears so that user can enter parameters inorder to get their value. Eg: IF a user enters name and click on OK then John will come as alert, or if user enters accountID then 123456789 will come as alert. This is working for me .

  if ( atts[0] !== '' ) {
    param = prompt( 'Enter desired URL parameter:' );

    for ( i = 0; i < atts.length; i++ ) {
        pair = atts[ i ].split( '=' );

        if ( pair[0] === param ) {
            alert( pair[1] );
            return false;
        }
    }

But the problem is when user types NAME (full caps) its not working . How to make this case insensitive. Mean NAME should be = name , or AcCOUntId should be = accountID . How to get this without using hard coded values (like name, email or accountID), URL can be anything . I need to match the values or strings which user enters.

Changing the comparison to following could help you:

if ( pair[0].toLowerCase() === param.toLowerCase() ) {
    alert( pair[1] );
    return false;
}

If you want to compare two strings case insensitive, you can lower or upper both strings like this:

if ( atts[0] !== '' ) {
    param = prompt( 'Enter desired URL parameter:' );

    for ( i = 0; i < atts.length; i++ ) {
        pair = atts[ i ].split( '=' );

        if ( pair[0].toLowerCase() === param.toLowerCase() ) {
            alert( pair[1] );
            return false;
        }
    }
}

See MDN for toLowerCase()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM